3 回答

TA贡献1860条经验 获得超8个赞
choices={"add","substract","multiply"}
while 1:
choice = input("Welcome to the basic calculator, please tell me if you want to add, substract or muliply: ")
if choice not in choices:
print("I think you made a typo, you'd have to try again.")
else: break
if choice == "add":
...
你在循环中完成输入和验证部分,然后你的代码进行计算。(假设计算器只计算一次然后退出。如果没有,你可以把整个事情放在另一个循环中进行更多计算,也许有一个退出命令。)
在我的示例中,验证是通过包含您可能的命令的集合(选择)完成的,并检查输入的成员资格。

TA贡献1815条经验 获得超13个赞
OP = ("add", "subtract", "multiply")
while True:
choice = input("Pick an operation {}: ".format(OP))
if choice not in OP:
print("Invalid input")
else:
break
if choice == OP[0]:
#...
添加回答
举报