我正在开始一个基本的文本冒险游戏。我得到了一个原型,但因为每个选项都是一个函数,所以我不知道如何安全地将我的位置保存到文件中
我考虑过将函数名保存到一个文件中,但是我想不出一个好的方法来在读取文件后将函数名作为str
来获取^任意str
上的{dict
将每个函数映射到它的名称是str
,但似乎随着更多选择的累积,这个dict
会使我的脚本膨胀
def choice1():
while True:
text = input("A or B?: ")
if text == "A":
return False, choice2
elif text == "B":
saygameover()
return True, None
elif askedforsave(text):
return True, choice1
else:
saytryagain()
def choice2():
while True:
text = input("C or D?: ")
if text == "C":
print("you win!")
return True, None
elif text == "D":
saygameover()
return True, None
elif askedforsave(text):
return True, choice2
else:
saytryagain()
def askedforsave(text):
if text == "save":
return True
else:
return False
def saytryagain():
print("try again...")
def saygameover():
print("game over.")
def play(choice = choice1):
done = False
while not done:
done, choice = choice()
if choice != None:
save(choice)
def save(choice):
pass
def load(file):
pass
return choice
这就是我目前得到的^当我已经在}可能是不必要的,但这是确保使用有效的Python名称而不是表达式的好方法
globals()
中检查名称时,{相关问题 更多 >
编程相关推荐