-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?

Eevee
  • 47,412
  • 11
  • 95
  • 127
jgamer578
  • 1
  • 1
  • 2
  • 3
    given that chapter 36 is called "Designing and Debugging", asking SO seems to be defeating the purpose... :) but here is a hint: variables you create in a function don't exist outside that function. – Eevee Oct 25 '13 at 00:44
  • From now on make it a point to give us the tracebacks also, so that we don't need to read the entire code – Pratik Singhal Oct 25 '13 at 03:51

2 Answers2

1

The problem is arising due to

    variable scope

You can check this question as well to clear your doubt regarding global or local scope:- Python global/local variables The variable

    has_dynamite

is defined only inside the

    start()

function. Therefore, it has only a local scope, to make it global define has_dynamite outside any functions and write your functions like this

    def start():
        global has_dynamite

Do this for all of the functions which use has_dynamite variable. You can also pass the has_dynamite variable as a parameter to the function. ie instead of typing the above mentioned code, you can type

    start(has_dynamite)

while calling the function, but be sure you include this parameter in the function definition also, otherwise it will show you an error Here is the corrected code:-

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

I hope this helps

Community
  • 1
  • 1
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
-1

has_dynamite is not global but local variable. It exists only in start()

Put it outside all functions to make it global.

has_dynamite = False
has_sword = False
has_bow = False

start()

Now you can get value of has_dynamite in any function

But if you need to change global value you have to use global keyword

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
furas
  • 134,197
  • 12
  • 106
  • 148