Python - 尝试/除了 sum 函数

我是 python 新手,我正在练习,所以我正在做一个 sum 函数,现在我想在输入不是我尝试过的数字的东西时显示一条消息错误try/excpet,它有点工作,但有时它没有,这是我的代码现在有:


def sum(num_1, num_2):

    sum_total = num_1 + num_2

    print('The answer is:',sum_total)

    return sum_total


def app_sum():

    try:

        inp_1 = int(input('Number 1?: '))

    except ValueError:

        print('Invalid input, try again')

        app_sum()


    try:

        inp_2 = int(input('Number 2?: '))

    except ValueError:

        print('Invalid input, try again')

        app_sum()


    sum(inp_1,inp_2)


app_sum()

这是我的输出:


Number 1?: 1

Number 2?: 2

The answer is: 3


Number 1?: A

Invalid input, try again

Number 1?: 1

Number 2?: A

Invalid input, try again

Number 1?: 1

Number 2?: 1

The answer is: 2

Traceback (most recent call last):

  File "suma.py", line 9, in app_sum

    inp_1 = int(input('Number 1?: '))

ValueError: invalid literal for int() with base 10: 'A'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "suma.py", line 24, in <module>

    app_sum()

  File "suma.py", line 12, in app_sum

    app_sum()

  File "suma.py", line 20, in app_sum

    sum(inp_1,inp_2)

UnboundLocalError: local variable 'inp_2' referenced before assignment


呼啦一阵风
浏览 228回答 2
2回答

慕田峪7331174

您收到此错误的原因是因为有一种情况,您将其拖到程序末尾,sum(inp_1,inp_2)没有实际定义 inp_1 或 inp_2。这是因为当您点击“除外”时,您又在运行该程序。这不会“杀死”失败的程序,它只是启动它的另一个实例。当该实例完成时,第一个实例也想结束,但正如我们之前提到的,它缺少一个变量,因此会引发错误。见堆栈;考虑一下食堂里盘子叠在一起的例子。位于顶部的板是第一个被移除的板,即已放置在最底部位置的板在堆叠中保留的时间最长......虽然最后一个“盘子”留在堆栈中的时间最长,但它仍然需要脱落。在我们的例子中,“最后一个盘子”是 app_sum() 的实例,它有错误的输入。当它离开堆栈时,它会为您提供您在帖子中包含的错误。试试这个:def sum(num_1, num_2):&nbsp; &nbsp; sum_total = num_1 + num_2&nbsp; &nbsp; print('The answer is:',sum_total)&nbsp; &nbsp; return sum_totaldef app_sum():&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inp_1 = int(input('Number 1?: '))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inp_2 = int(input('Number 2?: '))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Invalid input, try again')&nbsp; &nbsp; sum(inp_1, inp_2)app_sum()输出如下所示:root@alarmux:/home/abdmin/python# ./test.pyNumber 1?: 1Number 2?: 2The answer is: 3root@alarmux:/home/abdmin/python# ./test.pyNumber 1?: aInvalid input, try againNumber 1?: 2Number 2?: 3The answer is: 5

慕村9548890

您可以重复直到给出有效答案,而不是递归:def sum(num_1, num_2):&nbsp; &nbsp; sum_total = num_1 + num_2&nbsp; &nbsp; print('The answer is:',sum_total)&nbsp; &nbsp; return sum_totaldef app_sum():&nbsp; &nbsp; inp_1 = None&nbsp; &nbsp; while inp_1 is None:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inp_1 = int(input('Number 1?: '))&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Invalid input, try again')&nbsp; &nbsp; inp_2 = None&nbsp; &nbsp; while inp_2 is None:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inp_2 = int(input('Number 2?: '))&nbsp; &nbsp; &nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Invalid input, try again')&nbsp; &nbsp; sum(inp_1,inp_2)app_sum()示例输出:Number 1?: fjksdfsdflkjInvalid input, try againNumber 1?: 3Number 2?: fdsInvalid input, try againNumber 2?: 4The answer is: 7
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python