为什么我的while循环在Python中不起作用?

2024-04-27 22:46:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者。我试过使用while循环。我需要的代码,允许用户重新尝试的问题后,他们以前不正确的猜测。在第一次正确的尝试之后,它说出了我想要它说的话,但是在第一次错误的尝试之后,正确的答案是错误的。我如何解决这个问题?我做错了什么

    key = input("Choose a place and see if your guess is correct! ")
    if key == ("bed"):
        print("Congratulations! You are correct and will now progress to room 2.")
    while key != ("bed"):
        input("Unfortunately your guess is incorrect. Try again. ")        ```

Tags: andkey答案代码用户inputyourif
2条回答

首先,你需要缩进第二行。第二,循环不能工作,因为你说循环应该在钥匙“bed”时停止,但你不改变钥匙。第四行应该是:key = input("Unfortunately your guess is incorrect. Try again. ") 当然,您需要将if语句放入while循环中

while(True):
if key == ("bed"):
print("Congratulations! You are correct and will now progress to room 2.")
False
else:
key = input("Unfortunately your guess is incorrect. Try againg.")

也许像这样试试

相关问题 更多 >