一个简单的计算器程序的问题,输入数字零时似乎总是发生错误



我是堆栈溢出网站,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

帮助将不胜感激。谢谢!

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

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

这是给你错误的那条线。在计算 num2 之前,您应该检查 num2 是否为零num1 / 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")

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

注意这些线条

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

当你这样做时,它实际上是在执行计算并将其分配给相应的值(即商 = num1/num2 将实际进行计算并将结果存储在商变量中(。因此,无论您选择哪种运算符,您每次都在进行每次计算。最好仅在选择了如下所示的运算符时才分配值。

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得到绝对值,基本上去掉负值。

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

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

最新更新