Python全局名称';选择3';没有定义

Python全局名称';选择3';没有定义,python,Python,我正在尝试为一个类制作一个基于代码的冒险游戏,并不断收到一条错误消息,说“全局名称‘choice3’未定义”。我对编码非常陌生,所以我的代码可能有很多错误,我只是不确定问题出在哪里 def adventuregame(): global choice1 global choice2 global choice3 global choice4 global choic

我正在尝试为一个类制作一个基于代码的冒险游戏,并不断收到一条错误消息,说“全局名称‘choice3’未定义”。我对编码非常陌生,所以我的代码可能有很多错误,我只是不确定问题出在哪里

      def adventuregame():
            global choice1
            global choice2
            global choice3
            global choice4
            global choice2a
            global choice3a
            global choice4a
            print 'You are locked in a maximum security prison.'
            print 'You have a life sentence here and you want to escape.'
            print 'You’ve been considering ways to escape but you aren’t sure the best way to do it.' 
            choice1 = input('''Should you: 
            1 = Get some guys together and make a plan to fight the guards
            2 = Stick to yourself and make a plan to get out yourself
            Choice: ''')
            if choice1 == 1:
                print 'After talking to a few guys, you assemble a trustworthy team. Word gets around, and someone wants to join the plan that everyone seems to trust, but you don’t know him very well.'
                choice2 = input('''Should you:
                1 = Let him join
                2 = Don't let him join
                Choice: ''')
            if choice1 == 2:
                print 'You’re gonna have to figure a way out yourself. You could pickpocket a guard and get a key to your cell, or you could try and unlock the air vent in your room.'
                choice2a = input('''Should you:
                1 = Steal a key
                2 = Unlock the airvent
                Choice: ''')
                global choice2a
            elif choice2 == 1:
                print 'You trust your friends word and let him in on the plan. It’s probably a good idea to let him join, the more manpower the better.'
                print 'You and the team are discussing various ways of attacking. You get 2 periods a day of free time in the yard. During the morning there are less guards, but during the night it’s all newer guards that got stuck with the night shift.'   
                choice3 = input('''Should you:
                1 = Attack during the day
                2 = Attack during the night
                Choice: ''')
            if choice3 == 1:
                print 'You give the signal and attack. Even though there are less guards, it was still a brutal fight and you and one other person are the only ones to actually make it, and barely.'
            if choice3 == 2:
                print 'You give the signal and attack. The guards are surprised by what is happening, and not thinking clearly, don’t signal for other guards to come. Everyone makes it out before more guards arrive.'
            if choice3 == 1 or 2:
                print 'Once outside the prison, you have some choices on where to go. You can either blindly go through the woods for coverage and hope you come out somewhere, or follow along the river to where you know a town is.'
                choice4a = input('''Should you:
                1 = Go through the woods
                2 = Follow the river
                Choice: ''')
            elif choice2 == 2:
                print '''The guy rats you out to the guards and you get put in solitary confinement for a week, ruining your plans to escape. 
                GAME OVER'''
                restart = input('Type 1 to restart: ')
            if choice2a == 1:
                print '''You try and pickpocket a guard when he isn’t paying attention. You almost get away with it, but another guard notices and takes you down. You get put in solitary confinement and your plans of escaping are ruined. 
                                GAME OVER'''
                restart = input('Type 1 to restart: ')
            if choice4a == 2:
                print 'The guards sent out police helicopters, and you and your team are spotted and caught within an hour. GAME OVER'
                restart = input('Type 1 to restart: ')
            if choice4a == 1:
                print 'After several days in the woods, you come out to a small town. You use a payphone and call one of your friends that will help get you some money and out of the country. YOU WIN'
                restart = input('Type 1 to play again: ')
            if choice2a == 2:
                print 'You manage to get the air vent open, but you aren’t sure where any of the tunnels lead. You could just go for it and hope you find a way out, or you could wait until tomorrow and try and bribe a janitor for a map of the building.'
                choice3a = input('''Should you:
                1 = Try and find a way out
                2 = Bribe a janitor
                Choice: ''')
            if choice3a == 1:
                print 'You decide to risk it and hope you find a way out. After several hours of searching, you get lost and can’t even make it back to your room. You’re caught the next morning and put in solitary confinement. GAME OVER'
                restart = input('Type 1 to restart: ')
            if choice3a == 2:
                print 'After convincing a janitor to give up a map of the building in exchange for some contraband, you know exactly how to get out of the building. That night you make your escape.'
                print 'Once outside the prison, you have some choices on where to go. You can either blindly go through the woods for coverage and hope you come out somewhere, or follow along the river to where you know a town is.'
                choice4a = input('''Should you:
                1 = Go through the woods
                2 = Follow the river
                Choice: ''')
            if choice4a == 1:
                print 'After several days in the woods, you come out to a small town. You use a payphone and call one of your friends that will help get you some money and out of the country. YOU WIN'
                restart = input('Type 1 to play again: ')
            if choice4a == 2:
                print 'The guards sent out police helicopters, and you and your team are spotted and caught within an hour. GAME OVER'
                restart = input('Type 1 to restart: ')
            if restart == 1:
                adventuregame()     

如果您的代码中有那么多全局变量,您可能需要重新考虑您的设计,您还应该发现
elif
,而
loops您从未为choice3分配过任何内容……您在
If
块中分配
choice3
,但稍后调用
choice3
的值。如果代码从未进入该块,则该代码将没有指定值。它将具有值的唯一方法是如果
choice2==1
@Tgsmith61591我不需要代码具有值,除非'choice2==1'。即使当'choice2==1'时,它仍然会给我一条错误消息,表明choice3未定义。