我希望这个程序允许用户在游戏结束时重新启动游戏,如果他们不想再次玩游戏则退出游戏,但我无法弄清楚如何去做。能帮我解决一下这个程序的代码吗? 如果我输入以下代码,请更正我。 下面是我用python编写的游戏代码:
import random
secret_number = random.randrange(1, 101)
guess = 0
tries = 0
while guess != secret_number:
guess = int(input("Guess a number: "))
tries = tries + 1
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print("You got it!")
print("Number of tries: ", tries)
userInput = input("Enter 'R' to restart or 'X' to exit").capitalize()
if userInput == "R":
#code to restart the game goes here
elif gameReply == "X":
#code to exit the game goes here
答案 0 :(得分:0)
答案 1 :(得分:0)
要重新启动,您可以将尝试设置为零和continue
,因为您处于循环中。
if userInput == "R":
tries = 0
continue
退出时,sys.exit()
是赢家。
elif gameReply == "X":
sys.exit()