Dea*_*Iss 0 python loops input list while-loop
为什么这个非常简单的Python脚本不起作用?
我熟悉Java,所以我想我会给Python一个...但为什么这不起作用?
def playAgain(roundCounter):
reply = ""
replyList='y n'.split()
if roundCounter == 1:
print('Would you like to play again? Y/N')
while not reply in replyList:
reply = input().lower
if reply == 'y':
roundCounter == 1
elif reply == 'n':
print('Thanks for playing! Bye!')
sys.exit()
Run Code Online (Sandbox Code Playgroud)
这应该打印"你想再玩一次吗?" 然后继续请求用户输入,直到他们输入"Y"或"N".
出于某种原因,它会一遍又一遍地循环,并且不会突破循环 - 即使我键入'y'或'n'.
它是如此简单的一段代码,我不明白为什么它不起作用 - 实际上我在我的脚本中使用了一段几乎相同的代码,它运行良好!
你忘记了那些parantheses:
reply = input().lower # this returns a function instead of calling it
Run Code Online (Sandbox Code Playgroud)
做这个:
reply = input().lower()
Run Code Online (Sandbox Code Playgroud)
编辑:正如arshajii指出的那样,你的作业也是错误的:
if reply == 'y':
roundCounter == 1 # change this to: roundCounter = 1
Run Code Online (Sandbox Code Playgroud)
==是等于运算符并返回一个布尔值,赋值由=完成