我希望在我的骰子游戏结束时有一个选项:
你想重启吗?是或否
如果用户输入yes,游戏将重新启动并且无限次,直到用户已经有足够的游戏并退出。
我知道你可以用循环来做到这一点,但是怎么样?
[root@localhost libgraph-1.0.2]# yum install SDL.x86_64 SDL-devel.x86_64 SDL_image.x86_64 SDL_image-devel.x86_64
Redirecting to '/usr/bin/dnf install SDL.x86_64 SDL-devel.x86_64 SDL_image.x86_64 SDL_image-devel.x86_64' (see 'man yum2dnf')
Last metadata expiration check: 0:24:24 ago on Wed Jan 18 16:40:27 2017.
Package SDL-1.2.15-21.fc24.x86_64 is already installed, skipping.
Package SDL-devel-1.2.15-21.fc24.x86_64 is already installed, skipping.
Package SDL_image-1.2.12-14.fc24.x86_64 is already installed, skipping.
Package SDL_image-devel-1.2.12-14.fc24.x86_64 is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost libgraph-1.0.2]# dnf install Guile guile.x86_64 guile.i686 guile-devel.x86_64 compat-guile18-devel.x86_64
Last metadata expiration check: 0:25:13 ago on Wed Jan 18 16:40:27 2017.
No package Guile available.
Package guile-5:2.0.13-1.fc24.x86_64 is already installed, skipping.
Package guile-5:2.0.13-1.fc24.i686 is already installed, skipping.
Package compat-guile18-devel-1.8.8-13.fc24.x86_64 is already installed, skipping.
Error: Unable to find a match.
答案 0 :(得分:0)
您可以将游戏包装到函数中,例如game()
,并在无限循环中执行它。如果用户说“否”它退出循环,否则它会再次运行。等等...
while True:
game()
restart = input('do you want to restart Y/N?)
if restart == 'N'
break
elif restart == 'Y':
continue
答案 1 :(得分:0)
您可以使用while
循环来实现此选项。
以下是一个示例实现:
import random
# Instructions for starting the game
while True:
print("Type 'go' to roll")
dieroll = input()
if dieroll == 'go':
myNumber, pcNumber = random.randint(1,6), random.randint(1,6)
print("You rolled " + str(myNumber))
print("He rolled " + str(pcNumber))
if myNumber <= pcNumber:
print("You lose!")
else:
print("You win!")
print("Do you want to restart ? Yes or No")
response = input()
if not response == "Yes":
break
答案 2 :(得分:0)
首先,您应该将代码打包到函数中,例如
def game():
your code
对于用户提示,您可以在Python 3.x中使用raw_input() - Python 2.x和input() 在程序结束时尝试添加如下内容:
answer = raw_input("Restart?")
if answer == "Yes":
print "Leaving the game"
sys.exit(0) # import sys module
elif answer == "No":
print "Starting new game"
game()
答案 3 :(得分:0)
将程序划分为每个任务的单独函数是个好主意。它易于阅读和修改,因此可以轻松创建和实现重启功能。
函数restart()根据用户输入返回bool。主startGame循环在while循环的每次迭代中检查此值。
当用户想要停止播放(False)时,该功能返回并且程序停止。
import random
def rollDie():
myNumber = random.randint(1, 6)
pcNumber = random.randint(1, 6)
print("You rolled %d" % myNumber)
print("The PC rolled %d" % pcNumber)
if myNumber > pcNumber:
return "--- You win! ---"
else:
return "--- You lose! ---"
def restart():
user_input = input("Would you like to play again? Type 'Yes' or 'No'\n\n")
if user_input.lower() == "no": # Converts input to lowercase for comparison
return False
elif user_input.lower() == "yes":
return True
def startGame():
while True:
user_input = input("Dice Game: Try to roll a bigger number than the computer! Good Luck \n" +
"Type 'go' to roll the die... \n\n")
if user_input == "go":
print(rollDie())
if not restart():
return
startGame()
答案 4 :(得分:-1)
我刚刚提交了类似问题的帖子。如果这有帮助,请告诉我:
Simple Python Game Restart Solution
为了满足您的特定需求,您可以将重启代码更新为如下所示,以允许输入是/否输入选项。
play_again = input("Do you want to restart? Yes or No\n")
if play_again == "Yes":
exec(open("./yourfilenamehere.py").read())
else:
exit()