我是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. ") ```
首先,你需要缩进第二行。第二,循环不能工作,因为你说循环应该在钥匙“bed”时停止,但你不改变钥匙。第四行应该是:
key = input("Unfortunately your guess is incorrect. Try again. ")
当然,您需要将if语句放入while循环中也许像这样试试
相关问题 更多 >
编程相关推荐