我试图找到一种方法来访问confirmpath()函数中的pathcond()函数。请帮帮忙。
PS。大约一周前,我才开始学习Python,因此对于我的代码简洁性和提高我的整体技能的任何帮助都将是非常感谢的。
下面是我需要帮助的代码:
def name():
global call
call = raw_input("What is your name?\n")
print("Hello " + call)
def game():
global charchoose
charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n")
print("You chose " + charchoose)
def path():
pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower()
def confirmpath():
global confirmpath
confirmpath = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower()
pathcond()
confirmpath()
def pathcond():
while confirmpath == "no":
path()
if confirmpath == "yes":
print("Good choice, you win!")
else:
print("Sorry, we didn't get that. Can you answer again, please?")
confirmpath()
def ask():
askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
if askplay == "yes":
game()
path()
elif askplay == "no":
print("That's alright. Thanks for hanging out, though. Bye!")
else:
print("Sorry, I didn't get that. Please try again.")
ask()
name()
ask()更新:我在这个程序上做了一段时间的工作,最后得到了一个现在没有问题的版本。我把它贴在下面,请帮我把它做好,建议我可以修改/改进/删除什么。守则如下:
def Initiate():
global call
call = raw_input("What is your name?\n")
print("Hello " + call)
begin()
def game():
global charchoose
charchoose = raw_input("What will be your character " + call + ": Mage, Wizard or Knight?\n")
print("You chose " + charchoose)
path()
def path():
global pathchoose
pathchoose = raw_input("You are a " + charchoose + " who was walking down Tranversia and came across a three-way road. Which on will you choose? Land, Sea or Clouds\n").lower()
confirmpath()
def confirmpath():
global confirmpaths
confirmpaths = raw_input("You chose " + pathchoose + ". Are you sure you want to continue? Yes or No?\n").lower()
pathcond()
def pathcond():
while confirmpaths == "no":
path()
if confirmpaths == "yes":
print("Good choice, you win!")
else:
print("Sorry, we didn't get that. Can you answer again, please?")
confirmpath()
def begin():
askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
if askplay == "yes":
game()
elif askplay == "no":
print("That's alright. Thanks for hanging out, though. Bye!")
else:
print("Sorry, I didn't get that. Please try again.")
ask()
Initiate()更新2:代码正确运行,但仍然多次打印以下字符串
if confirmpaths == "yes":
print("Good choice, you win!")我观察到,它打印字符串的次数与响应confirmpath()函数的次数一样多,不管我的响应是什么。
发布于 2016-03-21 18:49:07
本质上,您正在尝试创建一个有限状态机。在Python中,最好使用类来实现这一点。这里有一个可能有用的相似实例。
类的重要部分是它可以跟踪游戏中的数据,并允许您避免全局变量。这是你的程序转换成一个类。
class Game:
def __init__(self):
self.name = raw_input("What is your name?\n")
print("Hello " + self.name)
self.ask()
def ask(self):
askplay = raw_input("Would you like to play a game? Yes or No?\n").lower()
if askplay == "yes":
self.game()
elif askplay == "no":
print("That's alright. Thanks for hanging out, though. Bye!")
else:
print("Sorry, I didn't get that. Please try again.")
self.ask()
def game(self):
self.charchoose = raw_input("What will be your character " + self.name + ": "
"Mage, Wizard or Knight?\n")
print("You chose " + self.charchoose)
self.path()
def path(self):
pathchoose = raw_input("You are a " + self.charchoose + " "
"who was walking down Tranversia and came across a three-way road. "
"Which on will you choose? Land, Sea or Clouds\n").lower()
self.confirmpath(pathchoose)
def confirmpath(self, pathchoose):
confirmpaths = raw_input("You chose " + pathchoose + ". "
"Are you sure you want to continue? Yes or No?\n").lower()
self.pathcond(confirmpaths, pathchoose)
def pathcond(self, confirmpaths, pathchoose):
if confirmpaths == "no":
self.path()
if confirmpaths == "yes":
print("Good choice, you win!")
else:
print("Sorry, we didn't get that. Can you answer again, please?")
self.confirmpath(pathchoose)
Game()当调用Game()时,将创建一个对象(实例)。类中的所有实例方法都引用self,它是该实例的名称。
虽然这似乎有点多,但类可能非常有用,并且是一个很好的概念,可以熟悉。
https://stackoverflow.com/questions/36138026
复制相似问题