一个简单的计算器程序的问题,输入数字零时似乎总是出错
Problem with a simple calculator program where an error always seems to occur when entering the number zero
我是堆栈溢出网站 python 和整个编程的新手。如果这个问题的标题或 body 不恰当,请原谅我。
我正在尝试在 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")
一切正常,除了当我输入 0 作为 num2 时,无论我 select 是什么运算符,输出
这是:
Traceback (most recent call last):
File "test4.py.txt", line 19, in <module>
quotient = num1 / num2
ZeroDivisionError: division by zero
帮助很大appreciated.Thanks!
求 if
条件内的商,而不是在检查 num==0
之前,否则除以 zero
已经完成,导致错误。:
elif operation == "/" :
if num2 == 0:
print("Sorry, can't divide a number by zero")
else:
quotient = num1 / num2
print(quotient)
您应该在检查运算符后才计算结果(从而避免尝试除以零而导致的任何错误),这样您也将避免不必要的计算:
elif operation == "/" :
if num2 == 0:
print("Sorry, can't divide a number by zero")
else:
quotient = num1 / num2
print(quotient)
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")
既然你是新手,我会更详细地解释这一点,这样你就知道你做错了什么
注意这些行
add = num1+num2
sub1 = num1-num2
sub2 = num2 - num1
product = num1*num2
quotient = num1 / num2
当你这样做时,它实际上是在执行计算并将其分配给相应的值(即 quotient = 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
取绝对值,基本去掉负数。
我是堆栈溢出网站 python 和整个编程的新手。如果这个问题的标题或 body 不恰当,请原谅我。
我正在尝试在 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")
一切正常,除了当我输入 0 作为 num2 时,无论我 select 是什么运算符,输出 这是:
Traceback (most recent call last):
File "test4.py.txt", line 19, in <module>
quotient = num1 / num2
ZeroDivisionError: division by zero
帮助很大appreciated.Thanks!
求 if
条件内的商,而不是在检查 num==0
之前,否则除以 zero
已经完成,导致错误。:
elif operation == "/" :
if num2 == 0:
print("Sorry, can't divide a number by zero")
else:
quotient = num1 / num2
print(quotient)
您应该在检查运算符后才计算结果(从而避免尝试除以零而导致的任何错误),这样您也将避免不必要的计算:
elif operation == "/" :
if num2 == 0:
print("Sorry, can't divide a number by zero")
else:
quotient = num1 / num2
print(quotient)
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")
既然你是新手,我会更详细地解释这一点,这样你就知道你做错了什么
注意这些行
add = num1+num2
sub1 = num1-num2
sub2 = num2 - num1
product = num1*num2
quotient = num1 / num2
当你这样做时,它实际上是在执行计算并将其分配给相应的值(即 quotient = 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
取绝对值,基本去掉负数。