为什么这段非常简单的Python脚本不起作用?而不是循环

为什么这段非常简单的Python脚本不起作用?而不是循环,python,list,loops,input,while-loop,Python,List,Loops,Input,While Loop,为什么这段非常简单的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

为什么这段非常简单的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()  
我熟悉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()  
这应该会打印出来,你想再玩一次吗?然后继续请求用户输入,直到他们键入“Y”或“N”

出于某种原因,它会一次又一次地循环,即使我输入“y”或“n”,也不会跳出循环


这是一段如此简单的代码,我不明白为什么它不起作用——事实上,我在我的脚本前面使用了一段几乎相同的代码,它工作得很好

你忘了那些妄想:

reply = input().lower  # this returns a function instead of calling it
这样做:

reply = input().lower()
编辑:正如arshajii所指出的,你的作业也做错了:

if reply == 'y':
    roundCounter == 1  # change this to: roundCounter = 1

==是相等运算符,返回一个布尔值,赋值由=

完成。您能共享这段几乎相同的代码吗?找出两者之间的差异可能会更容易找到问题。这与答案中解决的问题无关,但有助于Java=>Python的转换:您使用replyList='y n'.split做了太多的工作。有Python语法来定义一个更干净的列表replyList=['y','n']。如果您确实需要一个字符串,它可能是函数的一个参数,您可以像在列表中一样测试字符串中的成员身份。因此,reply_opts='yn',然后reply_opts中的'y'将计算为True。这似乎是一个比=='更大的问题,但为了完整性,您可能也想将其添加到您的答案中。Grrrr我删除了我的答案。Grrrr总是小事情!非常感谢。你也发现了我的另一个错误@arshajii,是的,请注意,反正roundCounter已经是1了,所以整个if reply=='y'是不必要的。事实上-通常roundCounter是6,但是为了测试我的代码,我将它减少到1!正如这个问题的提问者所指出的,如果回答不是“y”或“n”,他希望它循环。因此,在while循环中移动if和elif语句的想法毫无意义。他只想检查那些条件,如果回复确实在replyList中。它们在while循环之外非常好。阿什·辛格对这个问题的回答涵盖了一切。