This question already has answers here:
Why non-equality check of one variable against many values always returns true?
(3个答案)
去年关闭。
此代码不断返回
即使选择变量正确。我应该进行哪些编辑才能打印总成本?
相反,尝试类似
更好的是,使用您已有的列表:
(3个答案)
去年关闭。
sandwichType=""
totalCost=0
sandwiches=["Baguette", "Bagel", "Sub", "Wrap", "White"]
sandwichVAL={"Baguette":3.50, "Bagel":2.10, "Sub":3.99, "Wrap":4.15, "White":0.90}
choice=""
while choice!="Baguette" or choice!="Bagel" or choice!="Sub" or choice!="Wrap" or choice!="White":
choice=str(input("What type of sandwich would you like?\n>Baguette - £3.50\n>Bagel - £2.10\n>Sub - £3.99\n>Wrap - £4.15\n>White - £0.90\n"))
if choice!="Baguette" or choice!="Bagel" or choice!="Sub" or choice!="Wrap" or choice!="White":
print("Unfortunately, that is not a valid choice! Please pick again and remember to capitalise the first letter!")
else:
print()
totalCost+=sandwichVAL[choice]
print(totalCost)
此代码不断返回
Unfortunately, that is not a valid choice! Please pick again and remember to capitalise the first letter!
即使选择变量正确。我应该进行哪些编辑才能打印总成本?
最佳答案
看一下你的逻辑:
if choice != A or choice != B ...
choice
只能与您的一种硬编码替代项匹配;对于任何True
值,此测试必须为choice
。相反,尝试类似
if choice not in ["Baguette", "Bagel", "Sub", ...]:
更好的是,使用您已有的列表:
if choice not in sandwiches:
关于python - Python循环不断返回错误,我该如何修改? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48310130/
10-11 10:23