一段代码不能像预期的Python 3那样工作
代码:一段代码不能像预期的Python 3那样工作,python,python-3.x,Python,Python 3.x,代码: while True: print('who are you') name = input() if name != (str('joe')): continue print('hello joe whats the password?') if password == 'saltysea': break print ('access granted') 当我输入joe时,代码被卡住了:它一直在问“你是谁” 编辑:以下是我为达到预期效果所做的 pas
while True:
print('who are you')
name = input()
if name != (str('joe')):
continue
print('hello joe whats the password?')
if password == 'saltysea':
break
print ('access granted')
当我输入joe时,代码被卡住了:它一直在问“你是谁”
编辑:以下是我为达到预期效果所做的
password = '0'
while True:
print('who are you')
name=input()
if name != 'joe':
continue
print('hello joe whats the password?')
password=input()
if password == 'saltysea':
break
print ('access granted')
Python中的continue
语句将控件返回给
while循环的开始。continue语句拒绝所有
循环和移动的当前迭代中的剩余语句
控件返回到循环的顶部
(比较)
因此,在您的例子中,您正在从while顶部反复迭代到continue
语句。如果要在名称
不是joe之后停止循环执行,请使用中断
您的代码应该如下所示:
while True:
name = input('who are you')
if name != (str('joe')):
break
else:
password = input('hello joe whats the password?')
if password == 'saltysea':
print ('access granted')
break
我认为情况是,即使您输入
joe
I无法重现您的错误,循环也会不断重复。你输入的是“乔”吗?顺便说一句,if name!=(str('joe'))
是多余的<代码>“乔”已经是一个字符串!哈,这太奇怪了。。。好的,我仔细检查了我发布的代码是否正确,但我并不后悔我是新来的,但第二段代码是正确的