Python 名称错误:全局名称';有炸药';没有定义

Python 名称错误:全局名称';有炸药';没有定义,python,python-2.7,nameerror,Python,Python 2.7,Nameerror,我是编程新手。我正在学习python。我正在上第36课,它说做一个小游戏。这就是游戏: from sys import exit def you_won(): print """ CONGRATUFUCKINGLATIONS!!! You won this really easy but kind of cool game. This is my first game but not the last. Thanks for playing.

我是编程新手。我正在学习python。我正在上第36课,它说做一个小游戏。这就是游戏:

from sys import exit

def you_won():
    print """
    CONGRATUFUCKINGLATIONS!!!
    You won this really easy but kind of cool game. 
    This is my first game but not the last.
    Thanks for playing.
    Do you want to start over? Yes or No?
    """

    next = raw_input("> ")

    if next == "Yes" or "yes":
        start()
    elif next == "No" or "no":
        exit(0)
    else:
        try_again()

def try_again(function_name):
    print "That doesn't make sense, try again."
    function_name()

def youre_dead():
    print "You died, do you want to start over? Yes or No?"

    next = raw_input("> ")

    if next == "Yes" or "yes":
        start()
    elif next == "No" or "no":
        exit(0)
    else:
        try_again()

def main_room():
    print """
    You enter a grand room with a throne in the back.
    Standing right in front of you is the Qurupeco.
    He snarls at you and you bare your teeth.
    You can:
    1 Attack Qurupeco
    2 Taunt Qurupeco
    3 Run Away
    """ 
    if has_dynamite == True:
        print "A Use Dynamite"
    if has_sword == True:
        print "B Use Sword"       
    if has_bow == True:
        print "C Use Bow"

    next = raw_input("> ")

    if next == "1":
        "You attack the Qurupeco but he stabs you and carves your heart out with a spoon."
        youre_dead()
    elif next == "2":
        "You taunt the Qurupeco and he bites off your legs and lets you bleed to death."
        youre_dead()
    elif next == "3":
        print "You try to run away but the Qurupeco is too fast. He runs you down and eats your entire body."
        youre_dead()
    elif next == "A" or "a":
        print "You throw a stick of dynamite into his mouth and it blows him into tiny marshmallow size pieces."
        you_won()
    elif next == "B" or "b":
        "You slice him squarely in two equal pieces."
        you_won()
    elif next == "C":
        "You shoot him with your bow right between the eyes."
        you_won()      

def rightdoor_smallway():
    print """
    You go in the left door and see a lever at at the end of the room.
    The flooring looks weird in this room.
    You can:
    1 Pull Lever
    2 Leave
    """

    next = raw_input("> ")

    if next == "1":
        print """ 
        You pull the lever and feel the floor rush from beneath your feet.
        You fall for a while and land head first on a giant spike.
        """
        youre_dead()
    if next == "2":
        main_room()

def open_bow_chest():
    print "You open the chest and find a bow and some arrows."
    print "You take them and leave."

    has_bow = True

    main_room()

    return has_bow

def leftdoor_smallway():
    print """
    You open the door to your left and see a chest.
    You can:
    1 Open Chest
    2 Leave
    """

    next = raw_input("> ")

    if next == "1":
        open_bow_chest()
    elif next == "2":
        main_room()
    else:
        try_again()

def forward_threeway():
    print """
    You walk forward for a while until you see two doors.
    There is one to your right and one to your left.
    You can:
    1 Go Right
    2 Go Left
    3 Go Forward
    Note: If you go left, you can't go right.
    """

    next = raw_input("> ")

    if next == "1":
        rightdoor_smallway()
    elif next == "2":
        leftdoor_smallway()
    elif next == "3":
        main_room()

def three_way2():
    forward_threeway()    

def reason_meanman():
    print """
    You try to get the man to leave the damsel alone but he refuses.
    When you insist he gets mad and snaps your neck.
    """
    youre_dead()

def flirt_damsel():
    print """
    You flirt with the damsel, one thing leads to another and you end up having sex.
    It was great.
    Afterwards you tell her to wait for you there and you leave to finish your mission.
    """
    three_way2()

def free_damsel():
    print "You unchain the damsel, tell her where your king's castle is, and then leave."

    three_way2()

def punch_meanman():
    print """
    You punch the mean man and he falls through a large random spike.
    Ouch.
    The damsel says thank you and tries her best to be seductive.
    You can:
    1 Flirt With Her
    2 Free Her
    3 Leave Her There
    """

    next = raw_input("> ")

    if next == "1":
        flirt_damsel()
    elif next == "2":
        free_damsel()
    elif next == "3":
        three_way2()
    else:
        try_again()

def left_threeway():
    print """
    You go left for what seems like forever and finally see a door on the right.
    You enter the room and see a mean looking man terrorizing a damsel that is chained to a post.
    You can:
    1 Reason With Mean Man
    2 Punch Mean Man
    3 Run Away
    """

    next = raw_input("> ")

    if next == "1":
        reason_meanman()
    elif next == "2":
        punch_meanman()
    elif next == "3":
        three_way2()
    else:
        try_again()

def attack_gorilla():
    print "You run at the gorilla and he bites your head off."
    youre_dead()

def taunt_gorilla():
    print """
    You taunt the gorilla and he runs at you.
    You trip him and he smacks his head into the wall so hard that he dies.
    You can:
    1 Leave
    2 Open Chest
    """

    next = raw_input("> ")

    if next == "1":
        three_way2()
    elif next == "2":
        print "You open the chest, find a sword, take it, and leave."
        has_sword = True
        three_way2()
    else:
        try_again()

    return has_sword

def right_threeway():
    print """
    You go right for what seems like forever and finally find a door.
    You enter the room and see a giant gorilla gaurding a chest.
    You can:
    1 Attack Gorilla
    2 Taunt Gorilla
    3 Run Away
    """

    next = raw_input("> ")

    if next == "1":
        attack_gorilla()
    elif next == "2":
        taunt_gorilla()
    elif next == "3":
        three_way2()
    else:
        try_again()   

def back_threeway():
    print """
    You walk outside and hear a click sound.
    You try to open the door but it is now locked.
    You freeze to death.
    """

    youre_dead()

def three_way1():
    print """
    You enter the castle and see long hallways in every direction but behind you.
    You can:
    1 Go Forward
    2 Go Left
    3 Go Right 
    4 Go Back
    Note: You can only pick one of the last three.
    """

    next = raw_input("> ")

    if next == "1":
        forward_threeway()
    elif next == "2":
        left_threeway()
    elif next == "3":
        right_threeway()
    elif next == "4":
        back_threeway()
    else:
        try_again()

def go_inside():
    print "You go back to the entrance, open the door, and go inside."
    three_way1()

def poison_chest():
    print "You struggle with the chest for a minute and finally figure out the mechanism, but, unfortunately, a poisonous gas is released and you die in agony."
    youre_dead()

def outside_left():
    print """
    You go to the right and see a treasure chest three meters forward.
    You can:
    1 Go Inside
    2 Open Chest
    """

    next = raw_input("> ")

    if next == "1":
        turnback_outside()
    elif next == "2":
        poison_chest()
    else:
        try_again(outside_left)

def dynamite_inside():
    print "You take the dynamite, it may be useful."
    has_dynamite = True
    go_inside()
    return has_dynamite

def outside_right():
    print """
    You go left and see a stick of dynamite laying on the ground.
    You can:
    1 Go inside
    2 Take Dynamite
    """
    next = raw_input("> ")

    if next == "1":
        go_inside()
    elif next == "2":
        dynamite_inside()
    else:
        try_again(outside_right)        

def start():
    print """
    You are a knight in shining armor, you lost your sword on the way here.
    You have come to destroy the evil Qurupeco.
    You are at the cold entrance to the Qurupeco's castle.
    To the right of the entrance is a sign that reads: "Enter at your own risk!"
    The walls are made of extremely tough stone.
    You can:
    1 Enter Castle
    2 Go Left
    3 Go Right
    What do you do?
    Note: You can't go left and right.
    Note: Enter numbers when you're asked what you want to do.
    """

    has_dynamite = False
    has_sword = False
    has_bow = False

    next = raw_input("> ")

    if next == "1":
        three_way1()
    elif next == "2":
        outside_left()
    elif next == "3":
        outside_right()
    else:
        try_again(start)  

    return has_dynamite, has_sword, has_bow

start()  
这是我在玩游戏,却犯了一个我无法理解的错误

jacob@HP-DX2450:~/Documents$ python ex36.py

    You are a knight in shining armor, you lost your sword on the way here.
    You have come to destroy the evil Qurupeco.
    You are at the cold entrance to the Qurupeco's castle.
    To the right of the entrance is a sign that reads: "Enter at your own risk!"
    The walls are made of extremely tough stone.
    You can:
    1 Enter Castle
    2 Go Left
    3 Go Right
    What do you do?
    Note: You can't go left and right.
    Note: Enter numbers when you're asked what you want to do.

> 3

    You go left and see a stick of dynamite laying on the ground.
    You can:
    1 Go inside
    2 Take Dynamite

> 2
You take the dynamite, it may be useful.
You go back to the entrance, open the door, and go inside.

    You enter the castle and see long hallways in every direction but behind you.
    You can:
    1 Go Forward
    2 Go Left
    3 Go Right 
    4 Go Back
    Note: You can only pick one of the last three.

> 1

    You walk forward for a while until you see two doors.
    There is one to your right and one to your left.
    You can:
    1 Go Right
    2 Go Left
    3 Go Forward
    Note: If you go left, you can't go right.

> 3

    You enter a grand room with a throne in the back.
    Standing right in front of you is the Qurupeco.
    He snarls at you and you bare your teeth.
    You can:
    1 Attack Qurupeco
    2 Taunt Qurupeco
    3 Run Away

Traceback (most recent call last):
  File "ex36.py", line 368, in <module>
    start()  
  File "ex36.py", line 362, in start
    outside_right()
  File "ex36.py", line 331, in outside_right
    dynamite_inside()
  File "ex36.py", line 316, in dynamite_inside
    go_inside()
  File "ex36.py", line 290, in go_inside
    three_way1()
  File "ex36.py", line 278, in three_way1
    forward_threeway()
  File "ex36.py", line 140, in forward_threeway
    main_room()
  File "ex36.py", line 47, in main_room
    if has_dynamite == True:
NameError: global name 'has_dynamite' is not defined
jacob@HP-DX2450:~/Documents$ 
jacob@HP-DX2450:~/Documents$python ex36.py
你是一个穿着闪亮盔甲的骑士,你在来这里的路上丢了你的剑。
你是来消灭邪恶的库鲁佩科的。
你在库鲁佩科城堡冰冷的入口。
入口右侧有一块牌子,上面写着:“请自担风险!”
这些墙是用非常坚硬的石头砌成的。
你可以:
1.进入城堡
2向左走
3向右走
你是做什么的?
注意:你不能左右移动。
注意:当有人问你想做什么时,请输入数字。
> 3
你向左走,看到地上有一根炸药棒。
你可以:
我进去
2.拿炸药
> 2
你拿着炸药,它可能有用。
你回到入口,打开门,然后进去。
你进入城堡,可以看到四面八方都有长长的走廊,但在你身后。
你可以:
1前进
2向左走
3向右走
4回去
注意:您只能从最后三个选项中选择一个。
> 1
你向前走了一会儿,直到看到两扇门。
有一个在你的右边,一个在你的左边。
你可以:
我向右走
2向左走
3前进
注意:如果你向左走,你就不能向右走。
> 3
你进入一个后面有王座的大房间。
站在你面前的是库鲁佩科。
他对你咆哮,你却露出牙齿。
你可以:
1.库鲁佩科
2嘲弄库鲁佩科
3逃跑
回溯(最近一次呼叫最后一次):
文件“ex36.py”,第368行,在
开始()
文件“ex36.py”,第362行,在开始处
外(右)
文件“ex36.py”,第331行,在右外侧
炸药(内)
文件“ex36.py”,第316行,在dynamite_中
进去
文件“ex36.py”,第290行,在go_inside中
三方
文件“ex36.py”,第278行,以三种方式1
三向前进
文件“ex36.py”,第140行,三向前进
主控室()
文件“ex36.py”,第47行,在主控室
如果has_dynamite==True:
NameError:未定义全局名称“has_dynamite”
jacob@HP-DX2450:~/Documents$

有什么想法吗?

有炸药
不是全局变量,而是局部变量。它只存在于
start()

将其置于所有功能之外,使其全球化

has_dynamite = False
has_sword = False
has_bow = False

start()
现在,您可以在任何函数中获得
has_dynamite
的值

但如果需要更改全局值,则必须使用
global
关键字

def dynamite_inside():
    global has_dynamite

    print "You take the dynamite, it may be useful."
    has_dynamite = True
    go_inside()
    # return has_dynamite # unusable line

这个问题是由于

    variable scope
您也可以检查此问题,以澄清您对全球或本地范围的疑问:- 变量

    has_dynamite
仅在

    start()
功能。因此,它只有一个局部作用域,要使其成为全局作用域,就必须在任何函数之外定义dynamite并像这样编写函数

    def start():
        global has_dynamite
对使用has_dynamite变量的所有函数执行此操作。 还可以将has_dynamite变量作为参数传递给函数。ie,而不是键入上述代码,您可以键入

    start(has_dynamite)
调用函数时,请确保在函数定义中也包含此参数,否则将显示错误 以下是更正后的代码:-

        from sys import exit
has_dynamite = True
has_sword = True
has_bow = True  
def you_won():
    print """
    CONGRATUFUCKINGLATIONS!!!
    You won this really easy but kind of cool game. 
    This is my first game but not the last.
    Thanks for playing.
    Do you want to start over? Yes or No?
    """

    next = raw_input("> ")

    if next == "Yes" or "yes":
        start()
    elif next == "No" or "no":
        exit(0)
    else:
        try_again()

def try_again(function_name):
    print "That doesn't make sense, try again."
    function_name()

def youre_dead():
    print "You died, do you want to start over? Yes or No?"

    next = raw_input("> ")

    if next == "Yes" or "yes":
        start()
    elif next == "No" or "no":
        exit(0)
    else:
        try_again()

def main_room():
    global has_dynamite
    global has_sword
    global has_bow  
    print """
    You enter a grand room with a throne in the back.
    Standing right in front of you is the Qurupeco.
    He snarls at you and you bare your teeth.
    You can:
    1 Attack Qurupeco
    2 Taunt Qurupeco
    3 Run Away
    """ 
    if has_dynamite == True:
        print "A Use Dynamite"
    if has_sword == True:
        print "B Use Sword"       
    if has_bow == True:
        print "C Use Bow"

    next = raw_input("> ")

    if next == "1":
        "You attack the Qurupeco but he stabs you and carves your heart out with a spoon."
        youre_dead()
    elif next == "2":
        "You taunt the Qurupeco and he bites off your legs and lets you bleed to death."
        youre_dead()
    elif next == "3":
        print "You try to run away but the Qurupeco is too fast. He runs you down and eats your entire body."
        youre_dead()
    elif next == "A" or "a":
        print "You throw a stick of dynamite into his mouth and it blows him into tiny marshmallow size pieces."
        you_won()
    elif next == "B" or "b":
        "You slice him squarely in two equal pieces."
        you_won()
    elif next == "C":
        "You shoot him with your bow right between the eyes."
        you_won()      

def rightdoor_smallway():
    print """
    You go in the left door and see a lever at at the end of the room.
    The flooring looks weird in this room.
    You can:
    1 Pull Lever
    2 Leave
    """

    next = raw_input("> ")

    if next == "1":
        print """ 
        You pull the lever and feel the floor rush from beneath your feet.
        You fall for a while and land head first on a giant spike.
        """
        youre_dead()
    if next == "2":
        main_room()

def open_bow_chest():
    global has_bow 
    print "You open the chest and find a bow and some arrows."
    print "You take them and leave."

    has_bow = True

    main_room()

    return has_bow

def leftdoor_smallway():
    print """
    You open the door to your left and see a chest.
    You can:
    1 Open Chest
    2 Leave
    """

    next = raw_input("> ")

    if next == "1":
        open_bow_chest()
    elif next == "2":
        main_room()
    else:
        try_again()

def forward_threeway():
    print """
    You walk forward for a while until you see two doors.
    There is one to your right and one to your left.
    You can:
    1 Go Right
    2 Go Left
    3 Go Forward
    Note: If you go left, you can't go right.
    """

    next = raw_input("> ")

    if next == "1":
        rightdoor_smallway()
    elif next == "2":
        leftdoor_smallway()
    elif next == "3":
        main_room()

def three_way2():
    forward_threeway()    

def reason_meanman():
    print """
    You try to get the man to leave the damsel alone but he refuses.
    When you insist he gets mad and snaps your neck.
    """
    youre_dead()

def flirt_damsel():
    print """
    You flirt with the damsel, one thing leads to another and you end up having sex.
    It was great.
    Afterwards you tell her to wait for you there and you leave to finish your mission.
    """
    three_way2()

def free_damsel():
    print "You unchain the damsel, tell her where your king's castle is, and then leave."

    three_way2()

def punch_meanman():
    print """
    You punch the mean man and he falls through a large random spike.
    Ouch.
    The damsel says thank you and tries her best to be seductive.
    You can:
    1 Flirt With Her
    2 Free Her
    3 Leave Her There
    """

    next = raw_input("> ")

    if next == "1":
        flirt_damsel()
    elif next == "2":
        free_damsel()
    elif next == "3":
        three_way2()
    else:
        try_again()

def left_threeway():
    print """
    You go left for what seems like forever and finally see a door on the right.
    You enter the room and see a mean looking man terrorizing a damsel that is chained to a post.
    You can:
    1 Reason With Mean Man
    2 Punch Mean Man
    3 Run Away
    """

    next = raw_input("> ")

    if next == "1":
        reason_meanman()
    elif next == "2":
        punch_meanman()
    elif next == "3":
        three_way2()
    else:
        try_again()

def attack_gorilla():
    print "You run at the gorilla and he bites your head off."
    youre_dead()

def taunt_gorilla():
    print """
    You taunt the gorilla and he runs at you.
    You trip him and he smacks his head into the wall so hard that he dies.
    You can:
    1 Leave
    2 Open Chest
    """

    next = raw_input("> ")

    if next == "1":
        three_way2()
    elif next == "2":
        print "You open the chest, find a sword, take it, and leave."
        has_sword = True
        three_way2()
    else:
        try_again()

    return has_sword

def right_threeway():
    print """
    You go right for what seems like forever and finally find a door.
    You enter the room and see a giant gorilla gaurding a chest.
    You can:
    1 Attack Gorilla
    2 Taunt Gorilla
    3 Run Away
    """

    next = raw_input("> ")

    if next == "1":
        attack_gorilla()
    elif next == "2":
        taunt_gorilla()
    elif next == "3":
        three_way2()
    else:
        try_again()   

def back_threeway():
    print """
    You walk outside and hear a click sound.
    You try to open the door but it is now locked.
    You freeze to death.
    """

    youre_dead()

def three_way1():
    print """
    You enter the castle and see long hallways in every direction but behind you.
    You can:
    1 Go Forward
    2 Go Left
    3 Go Right 
    4 Go Back
    Note: You can only pick one of the last three.
    """

    next = raw_input("> ")

    if next == "1":
        forward_threeway()
    elif next == "2":
        left_threeway()
    elif next == "3":
        right_threeway()
    elif next == "4":
        back_threeway()
    else:
        try_again()

def go_inside():
    print "You go back to the entrance, open the door, and go inside."
    three_way1()

def poison_chest():
    print "You struggle with the chest for a minute and finally figure out the mechanism, but, unfortunately, a poisonous gas is released and you die in agony."
    youre_dead()

def outside_left():
    print """
    You go to the right and see a treasure chest three meters forward.
    You can:
    1 Go Inside
    2 Open Chest
    """

    next = raw_input("> ")

    if next == "1":
        turnback_outside()
    elif next == "2":
        poison_chest()
    else:
        try_again(outside_left)

def dynamite_inside():
    global has_dynamite 
    print "You take the dynamite, it may be useful."
    has_dynamite = True
    go_inside()
    return has_dynamite

def outside_right():
    print """
    You go left and see a stick of dynamite laying on the ground.
    You can:
    1 Go inside
    2 Take Dynamite
    """
    next = raw_input("> ")

    if next == "1":
        go_inside()
    elif next == "2":
        dynamite_inside()
    else:
        try_again(outside_right)        

def start():
    global has_dynamite
    global has_sword
    global has_bow 
    print """
    You are a knight in shining armor, you lost your sword on the way here.
    You have come to destroy the evil Qurupeco.
    You are at the cold entrance to the Qurupeco's castle.
    To the right of the entrance is a sign that reads: "Enter at your own risk!"
    The walls are made of extremely tough stone.
    You can:
    1 Enter Castle
    2 Go Left
    3 Go Right
    What do you do?
    Note: You can't go left and right.
    Note: Enter numbers when you're asked what you want to do.
    """

    has_dynamite = False
    has_sword = False
    has_bow = False

    next = raw_input("> ")

    if next == "1":
        three_way1()
    elif next == "2":
        outside_left()
    elif next == "3":
        outside_right()
    else:
        try_again(start)  

    return has_dynamite, has_sword, has_bow

start()  

我希望这能有所帮助

鉴于第36章被称为“设计和调试”,问这样的问题似乎违背了目的……:)但这里有一个提示:在函数中创建的变量不存在于该函数之外。从现在开始,我们还需要提供回溯,这样我们就不需要阅读整个代码