2013-10-25 176 views
-1

我是新手编程。我正在学习从here python。我正在上第36堂课,里面说要做一个小游戏。这是游戏:NameError:全局名称'has_dynamite'未定义

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() 

这是我玩游戏,并得到一个错误,我不知道。

[email protected]:~/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 
[email protected]:~/Documents$ 

任何想法?

+3

考虑到第36章被称为“设计和调试”,要求SO似乎击败目的... :)但这里是一个暗示:变量,你打造在一个函数外面不存在功能。 – Eevee

+0

从现在开始,让它成为我们追溯的一点,以便我们不需要读取整个代码 –

回答

-1

has_dynamite不是全局的而是局部变量。它只存在于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 
1

问题是引起由于

variable scope 

你也可以检查这个问题以及c李尔您的疑问,关于全局或局部范围: - Python global/local variables 变量

has_dynamite 

仅在

start() 

函数中定义。因此,它只有一个局部范围,使其全球定义has_dynamite之外的任何功能,写你的函数类似这样的

def start(): 
     global has_dynamite 

这样做对所有的这些使用has_dynamite变量的函数。 您也可以将has_dynamite变量作为参数传递给函数。即而不是键入上述代码,你可以键入

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() 

我希望这有助于

相关问题