Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 一个简单的计算器程序的问题,当输入数字0时似乎总是出现错误_Python 3.x_Python 3.6 - Fatal编程技术网

Python 3.x 一个简单的计算器程序的问题,当输入数字0时似乎总是出现错误

Python 3.x 一个简单的计算器程序的问题,当输入数字0时似乎总是出现错误,python-3.x,python-3.6,Python 3.x,Python 3.6,我对stack overflow网站、python以及整个编程都是新手。如果这个问题的标题或主体不合适,请原谅 我试图用python创建一个简单的计算器程序,它只执行四个运算,即加法、减法、差分、乘法和除法 这是我的密码: print("Welcome to the calculator \n") num1 = int(input("Enter the first number \n")) num2 = int(input("Enter the

我对stack overflow网站、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,并且无论我选择哪个操作符,输出 这是:


非常感谢您的帮助。谢谢

在if条件中查找商,而不是在检查num==0之前,否则已完成零除操作,导致错误:

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

在if条件中查找商,而不是在检查num==0之前,否则已完成零除操作,导致错误:

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)

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

elif operation == "/" :
    if num2 == 0: 
        print("Sorry, can't divide a number by zero")
    else:
        quotient = num1 / num2
        print(quotient)
这是给你错误的那一行。在计算num1/num2之前,应该检查num2是否为零。您可以按如下方式执行,您的程序将运行良好

quotient
if num2 == 0:
    quotient = None
else :
    quotient = num1 / num2
否则,您可以在用户输入运算符时声明商并计算商。若用户输入/,则可以检查num2==0,若为,则可以给出错误消息

工作代码- 这是给你错误的那一行。在计算num1/num2之前,应该检查num2是否为零。您可以按如下方式执行,您的程序将运行良好

quotient
if num2 == 0:
    quotient = None
else :
    quotient = num1 / num2
否则,您可以在用户输入运算符时声明商并计算商。若用户输入/,则可以检查num2==0,若为,则可以给出错误消息

工作代码-
因为你是新来的,我会更详细地解释这一点,这样你才知道自己做错了什么

注意这些线路

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

因为你是新来的,我会更详细地解释这一点,这样你才知道自己做错了什么

注意这些线路

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获取绝对值,基本上去除负数。

我认为您希望它出现在else块中,而不是if块中,在if块中它总是会引发异常。肯定需要出现在else块中我认为您希望它出现在else块中,不在if中,它总是会引发异常。肯定需要在else块中它是异常它是异常如果在检查运算符之前进行计算,请澄清为什么我似乎只在除法部分出错?因为你可以执行所有其他的加、减、乘运算,但是除以0是一个无效的操作。请你澄清一下,如果计算是在检查运算符之前完成的,那么为什么我似乎只在除法部分出错?因为你可以执行所有其他的加、减、乘操作,但是除以0是一个无效的操作。谢谢!我现在明白了,事先对所有操作执行计算是多余的。您能否澄清一下,python为什么不考虑:if num2==0语句进行零除?您的意思是什么?@blackdotso它确实考虑了这一点。但是在你的代码中,你已经在商数=num1/num2的行上除以零了,这就是为什么它会给你一个错误。你的代码目前是如何设置的,它甚至从来没有到达那个语句。谢谢!我现在明白了,预先为所有操作执行计算是多余的。您还可以澄清一下为什么python不考虑div的:if num2==0语句吗
“零伊森”是什么意思?@blackdotso确实考虑到了这一点。但是在你的代码中,你已经在商数=num1/num2的行被零除了,这就是为什么它会给你一个错误,你的代码目前是如何设置的,它甚至从来没有到达那个语句。