-1

I'm new to programming. I'm learning python from here. I am on lesson 36 which says to make a small game. This is the game:

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

This is me playing the game and getting an error that I cant figure out.

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$ 

Any ideas?

4

2 回答 2

1

问题是由于

    variable scope

您也可以检查此问题以清除您对全局或局部范围的疑问:- Python 全局/局部变量 变量

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

我希望这有帮助

于 2013-10-25T03:07:33.007 回答
-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
于 2013-10-25T00:55:55.157 回答