我必须在python 3上做这个计算器,该计算器必须在用户输入exit
之前循环执行。此外,在用户键入无效字符(例如要求输入操作员的字母或数字)之前,请进行编程。
我必须使用while
,if
...。但是,我不能使用import
。
while True:
operator = input("Give me an operator or \"exit\" to stop : ")
if operator == "exit":
break
if operator != "+" or "-" or "*" or "/":
print ("You must enter an operator")
break
no1 = input("Enter a number: ")
no2 = input("Enter a number: ")
if operator == "+":
output = int(num1) + int(num2)
elif operator == "-":
output = int(num1) - int(num2)
elif operator == "*":
output = int(num1) * int(num2)
else :
output = int(num1) / int(num2)
print("The result is " + str(result))
print("See you soon!")
我希望当我们输入除运算符之外的任何内容时,它实际上不会停止,我希望它循环回到:
operator = input("Give me an operator or \"exit\" to stop : ")
答案 0 :(得分:1)
在此之下,您会找到可以正常工作的代码,但是让我们从一般规则入手。
while True:
operator = input("Give me an operator or \"exit\" to stop : ")
if operator == "exit":
break
if operator not in [ "+", "-", "*" , "/"]: #<== here you condition is wrong ,do this instead
print ("You must enter an operator")
continue
try:
num1 = int(input("Enter a number: ")) #
num2 = int(input("Enter a number: ")) # <== both of this variable are not use else where ,rename to be consitdnt with the rest of your code
except ValueError:
print("Please enter an integer")
if operator == "+":
output = num1 + num2
elif operator == "-":
output = num1 - num2
elif operator == "*":
output = num1 * num2
else :
output = num1 / num2
print("The result is " + str(output)) #<=== here also, results was not defined
print("See you soon!")
答案 1 :(得分:0)
放入通用的 else
语句,该语句将在下一次迭代中继续。
if condition1 :
//logic
elif condition2 :
//logic
else:
continue
答案 2 :(得分:0)
您的问题是您在做什么:
if operator != "+" or "-" or "*" or "/":
真正在做的是:
if operator != "+" or 45 or 42 or 47:
(这些字符的ASCII表示形式)
这意味着无论如何条件都成立,因为or n
的N不为0会随时通过。
您要:
if operator != "+" and operator != "-" and operator != "*" and operator != "/":
您需要一个AND门。
此外,我注意到您说的是no1 = input(...)
,然后是int(num1)
,而不是int(no1)
。
要跳回输入,请使用continue
最终代码:
while True:
operator = input("Give me an operator or \"exit\" to stop : ")
if operator == "exit":
break
if operator not in [ "+", "-", "*" , "/"]: # credit to Florian Bernard for this line :)
print ("You must enter an operator")
continue
num1 = input("Enter a number: ")
num2 = input("Enter a number: ")
if operator == "+":
output = int(num1) + int(num2)
elif operator == "-":
output = int(num1) - int(num2)
elif operator == "*":
output = int(num1) * int(num2)
else:
output = int(num1) / int(num2)
print("The result is " + str(output)) # this was previously str(result) but result is was not defined
print("See you soon!")
但是!如果您今天感觉很新,可以使用PEP 572中引入的{strong> walrus运算符,该语言可以从python 3.8
获得while (operator := input("Give me an operator or \"exit\" to stop : ")) != "exit":
if operator not in [ "+", "-", "*" , "/"]: # credit to Florian Bernard for this line :)
print ("You must enter an operator")
continue
num1 = input("Enter a number: ")
num2 = input("Enter a number: ")
if operator == "+":
output = int(num1) + int(num2)
elif operator == "-":
output = int(num1) - int(num2)
elif operator == "*":
output = int(num1) * int(num2)
else:
output = int(num1) / int(num2)
print("The result is " + str(output))
print("See you soon!")
编辑:
编辑了最终代码以使用continue
,以前的版本是。
还添加了python 3.8实现。
EDIT2:
只是纠正一些东西,使句子更真实。