我刚刚开始学习Python,我对while循环有一些问题。
instruction_yes_no = ""
while instruction_yes_no.lower() != "y" or "n":
instruction_yes_no = input("Do you want to see the instruction? Please write 'Y' or 'N'\n")
if instruction_yes_no.lower() == "y":
print("You are gonna lose even if you read the instructions...")
print("\n")
time.sleep(1)
instruction()
elif instruction_yes_no.lower() == "n":
print("Do you think you are better than me? I will beat you faster since you have not read the instructions")
time.sleep(1)
else:
print("You mortal...you have not chosen a valid input. Type or 'Y' or 'N'")
time.sleep(1)
break
基本上,我想获得以下几点:
1)如果用户输入'y',则调用指令()函数(这是有效的)
2)如果用户输入'n',它会打印“你认为你比我好吗?...”(这是可行的)
3)如果用户没有键入'y‘或'n',我希望继续循环,直到用户插入或'y’或'n‘。然而,这是不起作用的。
我不明白为什么。我认为它应该是这样工作的:
'y‘or 'n’
发布于 2018-06-10 07:39:00
哦,这是一个典型的常见错误:
while instruction_yes_no.lower() != "y" or "n":
它与
while (instruction_yes_no.lower() != "y") or True:
您需要的是以下内容:
while instruction_yes_no.lower() != "y" and instruction_yes_no.lower() != "n":
也可以这样,它更短:)
while instruction_yes_no.lower() not in ["y", "n"]:
https://stackoverflow.com/questions/50779391
复制相似问题