【问题标题】:Problem with a simple calculator program where an error always seems to occur when entering the number zero一个简单的计算器程序的问题,输入数字零时似乎总是出现错误
【发布时间】:2020-06-24 17:01:16
【问题描述】:

我是堆栈溢出网站、python 和整个编程的新手。如果这个问题的标题或正文不恰当,请原谅我。

我正在尝试用python创建一个简单的计算器程序,它只执行四种操作,即加法、减法(差)、乘法和除法。

这是我的代码:

print("Welcome to the calculator \n")

num1 = int(input("Enter the first number \n"))
num2 = int(input("Enter the second number \n"))

operation = (input("""Enter the symbol of the operation to be performed. Your choices are:
                                             +
                                             - (Difference)
                                             *
                                             /
\n """))

add = num1+num2

sub1 = num1-num2
sub2 = num2 - num1

product = num1*num2
quotient = num1 / num2


if operation == "-" :

    if num1 > num2:
        print(sub1)
    else:
        print(sub2)

elif operation == "+" :
    print(add)

elif operation == "*" :
    print(product)

elif operation == "/" :
    if num2 == 0:
        print("Sorry, can't divide a number by zero")
    else:
        print(quotient)

else:
    print("Please enter a valid operator from among the ones provided above")

一切运行良好,除了当我输入零作为 num2 时,无论我选择什么运算符,输出 这是:

Traceback (most recent call last):
  File "test4.py.txt", line 19, in <module>
    quotient = num1 / num2
ZeroDivisionError: division by zero

非常感谢您的帮助。谢谢!

【问题讨论】:

  • 这是个例外

标签: python-3.x python-3.6


【解决方案1】:

if条件内的商,而不是在检查num==0之前,否则zero的除法已经完成,导致错误。:

elif operation == "/" :
    if num2 == 0:
        print("Sorry, can't divide a number by zero")
    else:
        quotient = num1 / num2
        print(quotient)

【讨论】:

  • 我认为您希望它在 else 块中,而不是在总是引发异常的 if 中。
  • 肯定需要在 else 块中
【解决方案2】:
quotient = num1 / num2

这就是给你错误的那一行。在计算 num1 / num2 之前,您应该检查 num2 是否为零。您可以按照以下方式进行操作,您的程序会正常运行。

quotient
if num2 == 0:
    quotient = None
else :
    quotient = num1 / num2

否则,您可以只声明商并在用户输入运算符时计算商。如果用户输入/,那么你可以检查是否num2==0,如果是,你可以给出错误信息。

工作代码 -

print("Welcome to the calculator \n")

num1 = int(input("Enter the first number \n"))
num2 = int(input("Enter the second number \n"))

operation = (input("""Enter the symbol of the operation to be performed. Your choices are:
                                             +
                                             - (Difference)
                                             *
                                             /
\n """))

add = num1+num2

sub1 = num1-num2
sub2 = num2 - num1

product = num1*num2
quotient = None     # Don't divide by num2 here or you will get error of divisionbyzero

if operation == "-" :

    if num1 > num2:
        print(sub1)
    else:
        print(sub2)

elif operation == "+" :
    print(add)

elif operation == "*" :
    print(product)

elif operation == "/" :
    if num2 == 0:
        print("Sorry, can't divide a number by zero")
    else:
        quotient = num1 / num2
        print(quotient)

else:
    print("Please enter a valid operator from among the ones provided above")

【讨论】:

    【解决方案3】:

    由于你是新手,我会更详细地解释这一点,以便你真正知道自己做错了什么

    注意这几行

    add = num1+num2
    
    sub1 = num1-num2
    sub2 = num2 - num1
    
    product = num1*num2
    quotient = num1 / num2
    

    当您执行此操作时,它实际上是在执行计算并将其分配给相应的值(即 quotient = num1 / num2 实际上将执行计算并将结果存储在 quotient 变量中)。因此,无论您选择何种运算符,您每次都在进行每项计算。最好只有在选择了该运算符的情况下才分配值,如下所示。

    print("Welcome to the calculator \n")
    
    num1 = int(input("Enter the first number \n"))
    num2 = int(input("Enter the second number \n"))
    
    operation = (input("""Enter the symbol of the operation to be performed. Your choices are:
                                                 +
                                                 - (Difference)
                                                 *
                                                 /
    \n """))
    
    
    if operation == "-" :
        sub1 = num1-num2
        sub2 = num2 - num1
        if num1 > num2:
            print(sub1)
        else:
            print(sub2)
    
    elif operation == "+" :
        add = num1+num2
        print(add)
    
    elif operation == "*" :
        product = num1*num2
        print(product)
    
    elif operation == "/" :
        if num2 == 0:
            print("Sorry, can't divide a number by zero")
        else:
            quotient = num1 / num2
            print(quotient)
    
    else:
        print("Please enter a valid operator from among the ones provided above")
    

    这部分只是一个建议。您还可以使用以下方法使您的差异操作更容易。

    if operation == "-" :
        sub1 = num1-num2
        print(abs(sub1))
    

    abs得到绝对值,基本去掉负数。

    【讨论】:

    • 谢谢!我现在明白,事先对所有操作进行计算是多余的。
    • 您能否解释一下为什么 python 不考虑: if num2 == 0 被零除的语句?
    • 什么意思?
    • @blackdotso 它确实考虑到了这一点。但是在您的代码中,您已经在 quotient = num1 / num2 行被零除,这就是它给您错误的原因
    • 您的代码当前是如何设置的,它甚至从未出现在该语句中。
    【解决方案4】:

    您应该在检查运算符后才计算结果(从而避免因试图除以零而导致的任何错误),这样您也可以避免不必要的计算:

    elif operation == "/" :
        if num2 == 0: 
            print("Sorry, can't divide a number by zero")
        else:
            quotient = num1 / num2
            print(quotient)
    

    【讨论】:

    • 如果在检查运算符之前完成计算,为什么我似乎只得到除法部分的错误?
    • 因为您可以执行所有其他操作(加、减、乘),但除以 0 是无效操作。
    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多