我正在尝试在“用Python发明你自己的游戏”一书中创建一个基于龙王国游戏的任务。原版允许做出一个选择,并使用函数来设置这些选择。我试图允许两个不同的选择,但不知道如何在第一个选择后停止任务,如果def语句给出了一个特定的结果,即。如果第一个选择是错误的选择,则用户不能进行第二个选择。任何帮助都将不胜感激。谢谢。
import random
import time
def displayIntro():
#introduction
def chooseDoor():
#user chooses door
def checkDoor(chosenDoor):
#checks user choice against random number
if chosenDoor == str(friendlyDoor):
#various good stuff happens
else:
#bad stuff happens
#at this point I would like to go back to the option to play again if they have chosen the wrong door
#but I can't make it work by putting a break here as it goes on to the next def statement (chooseBox)
#this is where the original game finished
def chooseBox():
#user chooses a box but only if they made the correct choice above
def checkBox(chosenBox):
#checks user choice against random number and good or bad stuff happens
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
displayIntro()
doorNumber = chooseDoor()
checkDoor(doorNumber)
boxNumber = chooseBox()
checkBox(boxNumber)
#play again option发布于 2014-07-21 17:58:22
如果选择了友好的门,则checkDoor()函数应返回True,否则返回False。然后,在主循环中修改
checkDoor()至
if not checkDoor():
continue这样,玩家选择了一个不友好的门,函数返回False,"continue“语句将程序重置为while循环的下一次执行。否则,它将继续执行chooseBox()
https://stackoverflow.com/questions/24862196
复制相似问题