我还是Python的新手,现在正在构建通用的入门猜谜游戏。我已经尝试为用户提供一个问题,如果他们希望继续,那么可惜的是它似乎无法识别我设定的答案。
def game():
numofguesses = 0
number = 10
while numofguesses < 5:
guess = int(input("Enter a number: "))
numofguesses = numofguesses + 1
if guess < number:
print("Higher")
elif guess > number:
print("Lower")
elif guess == number:
break
if guess == number:
print("You've won in " + str(numofguesses) + " guesses.")
else:
print("The correct number was " + str(number) + " and sadly you didnt get it!")
print("Hello, this game lets you guess a random number!")
go_again = True
while go_again:
game()
again = input("Do you want to play again? Y/N: ")
if again == "y" or "Y":
go_again = True
elif again == "n" or "N":
go_again = False
else:
print("unknown entry.")
当我想继续并输入“ y”时,它可以正常工作,但是即使我要退出并输入“ n”,我也得到相同的结果,程序会继续。
Hello, this game lets you guess a random number!
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N: y
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N: n
Enter a number: 10
You've won in 1 guesses.
Do you want to play again? Y/N:
我认为我错过了一件非常简单的事情,但是我一生都无法弄清楚它是什么。
非常感谢您的帮助!
问候