私は最初の python プロジェクトとしてテキスト アドベンチャーに取り組んでいます。テンプレートを使用しています (YouTube チュートリアルのコードをコピーします)。しかし、ゲームループを作成する代わりに、プレーヤーがコマンドを入力したときに実行される関数にしたいと考えています。(その部分は機能しています)。チュートリアルのコードは次のとおりです。
テキスト_アドベンチャー
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]
その部分は問題なく動作しますが、関数に変換しようとすると:
テキスト_アドベンチャー
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
何かを学ぶ最善の方法は、自分で答えを見つけることだということを私は知っています。私はしばらくの間探していましたが、どこにも行きません.どんな助けでも大歓迎です,ありがとう.