作为我的第一个 python 项目,我正在从事文本冒险。我正在使用模板(来自 youtube 教程的处理代码)。但不是创建一个游戏循环,我希望它是一个函数,在玩家输入命令时执行。(那部分正在工作)。这是教程中的代码:
Text_Adventure
bridge = ("Bridge", "You are on the bridge of a spaceship, sitting in the captains chair. ")
readyRoom = ("Ready Room" , "The captains ready room ")
lift = ("Lift" , "A turbolift that takes you throughout the ship. ")
transitions = {
bridge: (readyRoom, lift),
readyRoom: (bridge,),
lift: (bridge,)
}
location = bridge
while True:
print (location[1])
print ("You can go to these places: ")
for (i, t) in enumerate(transitions[location]):
print (i + 1, t[0])
choice = int(input('Choose one: '))
location = transitions[location][choice - 1]
那部分工作正常,但是当我尝试将它变成一个函数时:
Text_Adventure
bridge = ("Bridge", "You are on the bridge of a spaceship, sitting in the captains chair. ")
readyRoom = ("Ready Room" , "The captains ready room ")
lift = ("Lift" , "A turbolift that takes you throughout the ship. ")
transitions = {
bridge: (readyRoom, lift),
readyRoom: (bridge,),
lift: (bridge,)
}
location = bridge
def travel():
print (location[1])
print ("You can go to these places: ")
for (i, t) in enumerate(transitions[location]):
print (i + 1, t[0])
choice = int(input('Choose one: '))
location = transitions[location][choice - 1]
travel()
我收到错误消息:
UnboundLocalError: local variable 'location' referenced before assignment
我知道学习东西的最好方法是自己找到答案。我一直在寻找一段时间,但没有得到任何地方,任何帮助将不胜感激,谢谢。