简体   繁体   中英

When i try to run this program i get this error: File “python”, line 33, in <module> NameError: global name 'hp' is not defined

I don't know why this isn't working; it comes up with the error:
File "python", line 33, in <module> NameError: global name 'hp' is not defined

def handle_room(location):
    global hp

    if location == "start":
        print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your right.")
        direction = raw_input("Do you want to go left or right?")
        if direction == "left":
            return "cave"    
        elif direction == "right":
            return "beach"
    elif location == "cave":
        print("You walk to the cave and notice there is an opening.")
        print("A small snake bites you, and you lose 20 health points.")
        hp = hp - 20        
        answer = raw_input("Do you want to go deeper?")
        if answer == "yes":
            return "deep_cave"
        else:
            return "start"
    elif location == "beach":
        print("You walk to the beach but remember you do not have any swimwear.")
        print("The cool water revitalizes you. You have never felt more alive, gain 70 health points.")
        hp += 70
        return "end"

    else:
        print("Programmer error, room ", location, " is unknown")
        return "end"
location = "start"
while location != "end":
    location = handle_room(location)
    print("You now have ", hp, "health points.")
    if hp <= 0:
        print("You are dead. I am sorry.")
print("Your adventure has ended. Goodbye.")

Can you guys help me out with this? I have tried a bunch with indents and stuff like that but nothing seems to work. If you could help me get sorted I would be very grateful.

just add hp=0 at the very next line.

location = "start"
def handle_room(location):
    global hp
    hp=0  # assigned a value to global variable

    if location == "start":
        print("You are standing on a path at the edge of a jungle. There is a cave to your left and a beach to your right.")
        direction = raw_input("Do you want to go left or right?")
        if direction == "left":
            return "cave"
        elif direction == "right":
            return "beach"
    elif location == "cave":
        print("You walk to the cave and notice there is an opening.")
        print("A small snake bites you, and you lose 20 health points.")
        hp = hp - 20
        answer = raw_input("Do you want to go deeper?")
        if answer == "yes":
            return "deep_cave"
        else:
            return "start"
    elif location == "beach":
        print("You walk to the beach but remember you do not have any swimwear.")
        print("The cool water revitalizes you. You have never felt more alive, gain 70 health points.")
        hp += 70
        return "end"

    else:
        print("Programmer error, room ", location, " is unknown")
        return "end"
location = "start"
while location != "end":
    location = handle_room(location)
    print("You now have ", hp, "health points.")
    if hp <= 0:
        print("You are dead. I am sorry.")
        break
print("Your adventure has ended. Goodbye.")

Like error says variable hp is not defined. Just add a declaration on top of your file.

hp = 100    #

You haven't initialized hp .

This works:

hp = 19
location = "start"
while location != "end":
    location = handle_room(location)
    print("You now have ", hp, "health points.")
    if hp <= 0:
        print("You are dead. I am sorry.")
        break
print("Your adventure has ended. Goodbye.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM