2

I was making a game where you need to fight monsters, just to practice my coding, when I got an error I couldn't understand. Here is the code, and the error I received:

Note: I changed it to the full code so you could see everything. Sorry about how long it is.

import random
import time

player_level = 1
player_HP = 4
player_mana = 2
player_action = "Placeholder"

entity_list = ["Nymph", "Komodo", "Free Soul"]
current_monster_level = player_level
current_monster_HP = random.randrange(current_monster_level, (current_monster_level * 3) + 1)
monster_name = entity_list[random.randrange(0, 3)]

def spawn_monster(level):
    import random
    global current_monster_HP
    global monster_name
    current_monster_HP = random.randrange(level, level * 3)
    monster_name = entity_list[random.randrange(0, 3)]
    print("A ", monster_name, "attacks you!")
    print("The ", monster_name, " has ", current_monster_HP, " HP!")

def attack(level):
    import random
    global monster_name
    global current_monster_HP
    damage = random.randrange(level, level + 4)
    current_monster_HP = current_monster_HP - damage
    if damage == 0:
        print("You missed! No damage done! ", monster_name, "'s HP is ", current_monster_HP, " !")
    elif damage > current_monster_HP:
        damage = current_monster_HP
        print("You did ", damage, " damage! The ", monster_name, " was defeated!")

    else:
        print("You did ", damage, " damage.  The enemy's HP is now ", current_monster_HP, "!")

def spell(level):
    global player_mana
    global player_HP
    player_mana = player_mana - 2
    player_HP = player_HP + level
    print("A spell is cast, and you gain ", level, "HP!")

def monster_retaliate(level):
    import random
    global player_HP
    global monster_name
    global current_monster_HP
    damage = random.randrange(level, level + 2)
    player_HP = player_HP - damage 
    print("The ", monster_name, " attacked you, dealing ", damage, " damage!")
    if damage == 0:
        print("The enemy missed! No damage done! Your HP is ", player_HP, "!")
    elif damage > current_monster_HP:
        damage = current_monster_HP
    else:
        print("the ", monster_name, " did ", damage, " damage. Your HP is now ", player_HP, "!")


print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("x Blind RPG x")
print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("\n")

begin = input("Start a new game?").lower()

if begin == "yes":
    print("Your tale starts when your memory does.  You can't remember anything!")
    time.sleep(1)
    print("You wake up in an empty field, and you think you see a shadowy figure.")
    time.sleep(1)
    print("It slips away into the faraway trees lining the grassy field, or does it?")
    time.sleep(1)
    print("You stand up.")
    time.sleep(1)
    print("You decide to go over to where the shadow left the field.")
    time.sleep(1)
    print("On the ground, you see a small sword.  You pick it up.")
    time.sleep(1)
    print("Suddenly, something jumps out at you from behind a tree.")
    time.sleep(1)

    spawn_monster(1)
    while current_monster_HP > 0:
        player_action = input("What do you do?").lower()
        if player_action == "attack":
            attack(player_level)
            time.sleep(2)
            if current_monster_HP > 0:
                monster_retaliate(1)
            else:
                break
        elif player_action == "cast spell":
            spell(player_level)
            time.sleep(2)
            monster_retaliate(1)
        else:
            print("The enemy attacked you as you stood unmoving!")
            monster_retaliate(1)

        if current_monster_HP <= 0:
            print("You win! Apparently you remember how to fight!")
            break

        if player_HP <= 0:
            print("You died!  If only you had remembered how to fight...")
            break
else:
    print("Then why did you open the game?")

The attack() function works, and the spell() function both work, it is only the monster_retaliate() function that is causing trouble. The Error code is:

File "/Users/(Replacing my name for privacy)/Documents/Blind RPG.py", line 57, in monster_retaliate

player_HP = player_HP - damage NameError: name 'player_HP' is not defined

I thought this was funny, seeing as I used the global keyword

1
  • 1
    If you want to practice your coding skills, try it without the globals. Globals cause problems, and make code less stable, harder to understand, and fix. Parameters are your friend ;) Commented Jul 15, 2016 at 15:18

1 Answer 1

5

global doesn't actually create a variable; it just indicates that the name should be defined in the global scope, and to modify that (instead of a local variable) when you assign to it. You still need to give it a value before you try to access it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.