Python 计算器执行循环直到;退出“;
我在Python3上完成了这个计算器,它必须在循环中执行,直到用户键入Python 计算器执行循环直到;退出“;,python,python-3.x,Python,Python 3.x,我在Python3上完成了这个计算器,它必须在循环中执行,直到用户键入exit。此外,在用户输入无效字符之前进行编程,例如要求输入运算符或数字时的字母 我必须使用while,if。。。但是,我无法使用导入 while True: operator = input("Give me an operator or \"exit\" to stop : ") if operator == "exit": break if operator != "+" or
exit
。此外,在用户输入无效字符之前进行编程,例如要求输入运算符或数字时的字母
我必须使用while
,if
。。。但是,我无法使用导入
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 : ")
放置一个通用的
else
语句,该语句将继续下一次迭代
if condition1 :
//logic
elif condition2 :
//logic
else:
continue
你的问题是你正在做:
if运算符!=“+”或“-”或“*”或“/”:
而它真正做的是:
if运算符!=“+”或45或42或47:
(这些字符的ASCII表示形式)这意味着,无论发生什么情况,条件都是真的,因为n不是0的情况下,
或n
会随时通过
您想要:if运算符!=“+”和运算符!=“-”和操作员!=“*”和运算符!=“/”:
你需要一个和门 另外,我注意到你说的是
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!")
但是!如果您今天感觉新鲜,您可以使用中引入的海象操作符,该操作符可从中获得
编辑:编辑最终代码以使用
继续
,以前的版本为。还添加了一个Python3.8实现 编辑2:
只是纠正一些东西,让句子更真实 您将在下面看到您的代码按预期工作,但是让我们从一般规则开始
- 注意,如果在声明变量时,在代码的其他地方使用相同的变量
- 在尝试编写脚本之前,测试代码的简单部分。你们测试操作员的条件完全被破坏了
为True时:
操作员=输入(“给我一个操作员或\“退出\”停止:)
如果运算符==“退出”:
打破
如果运算符不在[“+”、“-”、“*”、“/”]:#只需将第二个break
替换为continue
请注意这一行:operator=input(“给我一个运算符或\“exit\”停止):
,因为您应该会得到一个EOF
错误。您需要使用一条语句(而不是终止循环的break
).你的情况不好,请看一下我的眼镜。谢谢大家的帮助!但我仍然有一个问题:我希望当它被要求输入一个号码时,我们输入一个字母或运算符,它会说“你必须输入一个号码”,然后它会返回要求一个号码。我现在的问题是:如果运算符!=“+”或“-”或“*”或“/”:打印(“必须输入运算符”)。在这个块中有些东西不起作用,因为即使我写了一个有效的运算符,它仍然会打印“您必须输入一个运算符”,并且它会继续要求输入一个数字。我该如何让它工作?我需要做什么,这样当它请求一个数字时,当它收到一个字母或一个运算符时,它会说“你必须输入一个数字”,然后它会返回请求一个数字?@RayCactus要继续循环,跳过所有代码,请使用关键字continue
。我建议你再次阅读我的整个答案,我做了一些修改…@raycachtus还有,看看Florian Bernard的答案,检查输入是否不是运算符(不在[…]
部分),我非常喜欢它,比使用and更好,但我能做些什么,当它被要求输入一个数字时,我们输入一个字母或运算符,它会说“你必须输入一个数字”,然后它返回要求一个数字?更新,使用isalnum。我不能使用isalnum。有没有办法让它像:if not int(num1和num2):print(“请输入一个管理者”)??@raycachtus你能用一个try-except块吗?我能,请告诉我如何使用它
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!")