当我输入joe
或Joe
时,为什么我不能跳出第一个while循环?输入正确的参数不会让我跳出while循环
joe = ''
password = ''
while joe != 'Joe' or 'joe':
print('Who are you?')
joe = input()
while password != 'swordfish' or 'Swordfish':
if joe == 'joe' or 'Joe':
print('What is the password? (Hint: its a fish)')
password = input()
if joe == 'Joe' or 'joe' and password == 'swordfish' or 'Swordfish':
print('Access Granted')
else:
print('Access Denied')
这个'如果joe == '乔' 或“Joe''并不意味着你认为这意味着什么。它意味着'if(joe =='joe')或bool('Joe')'和'bool('Joe')'总是评估为'True',这意味着if语句总是通过。你可能的意思是“如果乔(乔','乔')'。或者甚至更好'如果joe.lower()=='joe''。 –
对于'while'循环中的条件,请尝试'joe!='Joe'或joe!='joe''。现在,while循环将继续运行,而“joe”不等于“Joe”或简单地“joe”。 ''joe''是一个字符串,它的布尔值总是'True'。因此,无论“joe”是否等于“Joe”,字符串“joe”将始终评估为“真”,而“while”循环永远不会中断。 – victor
嘿!感谢您的帮助。在我发布这篇文章之后,我看了一下我又有了什么。 (我想休息的时候头脑不错),我看到我没有两次写变量。你是对的'乔'没有乔=='乔'在while循环中没有意义。 我也使用了错误的操作符。它应该是一个“和”运算符,而不是一个或。感谢.lower()提示,这会缩短代码并使其更高效! :) –