[英]Python program that allows user to restart or exit it
I want this program to allow users to restart the game when they are through or exit it if they do not want to play the game again, but I cant figure out how to do it. 我希望该程序允许用户通过时重新启动游戏,或者如果不想再次玩游戏则退出该程序,但是我不知道该怎么做。 Can you please help me with the code that makes the program do that?
您能帮我提供使程序实现此目的的代码吗? Please correct me if I mistype the code below.
如果我输入以下代码,请纠正我。 Below is the code of the game I have written using python:
以下是我使用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
random.randrange()
while you are testing your program. random.randrange()
。 hello
? hello
,您想做什么? In the following program, you basically continue if it is not x
or X
and play again. x
或X
,则基本上可以继续然后再次播放。 Here is the fixed version: 这是固定版本:
import random
import sys
while True: # outer game loop
print('Welcome to the game.')
secret_number = random.randrange(1, 5)
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 == "X":
print('Goodbye.')
sys.exit(-1) # exits the program
To restart the game: Well, you can declare a function that initializes the game, and then you call it if you want to restart. 重新启动游戏:嗯,您可以声明一个用于初始化游戏的函数,然后在要重新启动时调用它。
To exit: 退出:
sys.exit() sys.exit()
I have a similar game already coded: you can have a look at it HERE . 我已经编写了一个类似的游戏:您可以在这里查看。 It was my first python code
这是我的第一个python代码
For restart, you can just set tries to zero and continue
since you are in a loop. 对于重新启动,由于处于循环中,您可以将尝试次数设置为零并
continue
。
if userInput == "R":
tries = 0
continue
For exit, sys.exit()
is a winner. 对于退出,
sys.exit()
是赢家。
elif gameReply == "X":
sys.exit()
You can use sys.exit() to stop the script and wrap your code into a method so you can call it again when the user wants to restart. 您可以使用sys.exit()停止脚本并将代码包装到一个方法中,以便在用户要重新启动时可以再次调用它。 Also, use raw_input rather than input as input is expecting Python code.
另外,使用raw_input而不是input作为期望Python代码的输入。
For example: 例如:
import random
import sys
def my_game():
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 = raw_input("Enter 'R' to restart or 'X' to exit").capitalize()
if userInput == "R":
my_game()
elif userInput == "X":
sys.exit()
my_game()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.