Python初学者。我一直在尝试让我的游戏在初始游戏完成后给用户一个“重玩”选项。如果我在6次尝试后猜不到数字,重放就可以工作了,但是如果我成功地猜到了数字并尝试重放,就什么都不会发生
import random
secretNumber = random.randint(1, 20)
userGuesses = 0
userInput = False
while userInput == False:
print("Let's play a game!")
print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
print("What is your first guess?")
while userGuesses <= 5:
userInput = input()
if int(userInput) > secretNumber:
print("Too High! Try again!")
userGuesses += 1
elif int(userInput) < secretNumber:
print("Too Low! Try again!")
userGuesses += 1
else:
print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
else:
userInput = True
print("Goodbye!")
else:
print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
"next time!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
else:
userInput = True
print("Goodbye!")
提前谢谢你的提示
只要在“重新启动”游戏的地方添加一个休息时间:
希望这对你有帮助
你也可以在一个单独的函数中定义你的游戏。我会这样做:
相关问题 更多 >
编程相关推荐