Я работаю над текстовым приключением в качестве своего первого проекта python. Я использую шаблон, (копирование кода из учебника youtube). но вместо создания игрового цикла я хочу, чтобы это была функция, которая должна выполняться, когда игрок вводит команду. (эта часть работает). Вот код из учебника:Локальная переменная 'location', на которую ссылаются перед приложением-приложением
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()
I получите сообщение об ошибке:
UnboundLocalError: local variable 'location' referenced before assignment
Я знаю, что лучший способ узнать что-то - найти ответ самостоятельно. Я искал какое-то время и не получаю нигде, Любая помощь будет принята с благодарностью, спасибо.
Можете ли вы опубликовать полную трассу? – mgilson
Traceback (последний последний звонок): Файл «C: \ Documents and Settings \ Kenneth \ My Documents \ Python_Projects \ projects \ TA.Project \ location \ places_simple.py ", строка 28, в travel() Файл« C: \ Documents and Settings \ Kenneth \ My Documents \ Python_Projects \ projects \ TA.Project \ location \ places_simple.py ", строка 19, в путешествии печать (location [1]) UnboundLocalError: локальная переменная 'location', на которую ссылаются перед присвоением [Закончено в 0,1 с кодом выхода 1] –
user2922016