coolleo63
coolleo63

Reputation: 1

Name 'enemy' not defined - Python

My code isn't working properly, it says that enemy is not defined when I have defined it:

class Gnome:
    def __init__(enemy, name):
        enemy.name = name
        enemy.health2 = 50
        enemy.health = enemy.health2
        enemy.attack = 7
        enemy.goldgain = 6
GnomeIG = Gnome("Gnome")

class Goblin:
    def __init__(enemy, name):
        enemy.name = name
        enemy.health2 = 38
        enemy.health = enemy.health2
        enemy.attack = 5
        enemy.goldgain = 3
GoblinIG = Goblin("Goblin")

class Wolf:
    def __init__(enemy, name):
        enemy.name = name
        enemy.health2 = 20
        enemy.health = enemy.health2
        enemy.attack = 3
        enemy.goldgain = 1
WolfIG = Wolf("Wolf")

This is the game:

def prefight():
    global enemy
    enemynum = random.randint(1, 3)
    if enemynum == "1":
        enemy = GnomeIG
    elif enemynum == "2":
        enemy = GoblinIG
    elif enemynum == "3":
        enemy = WolfIG
    fight()

All the lines with 'enemy.something' give the error that it is not defined.

def fight():
    print("You have encountered a %s!" % enemy.name)
    print("Player Life: %d/%d | Enemy Life: %i/%i" % (PlayerIG.health, 
           PlayerIG.health2, enemy.health, enemy.health2)) 
    print("Potions: %i\n" % PlayerIG.potions)
    print("1. Attack")
    print("2. Drink Potion")
    print("3. Run")
    option = input("> ")
    if option == "1":
        attack()
    elif option == "2":
        drinkpotion()
    elif option == "3":
        run()
    else:
        fight()

Error:

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 231, in

main()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 51, in main

start()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 65, in start

start1()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 84, in start1

wild()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 104, in wild

fight()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 118, in fight

print("You have encountered a %s!" % enemy.name)

NameError: name 'enemy' is not defined

Upvotes: 0

Views: 345

Answers (2)

igor Smirnov
igor Smirnov

Reputation: 321

Error:

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 231, in

main()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 51, in main

start()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 65, in start

start1()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 84, in start1

wild()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 104, in wild

fight()

File "C:/Users/Leo/PycharmProjects/RPG Proj/rpg.py", line 118, in fight

print("You have encountered a %s!" % enemy.name)

It look like you call main() -> start() -> start1() -> fight() and never actually call prefight(). So it never have a chance to introduce this variable named enemy.

Upvotes: 0

scrbbL
scrbbL

Reputation: 456

This line of code generates an integer between 1 and 3

enemynum = random.randint(1, 3)

Your conditionals, however, test against strings.

if enemynum == "1":

This means that none of your if or elif statements will ever be entered and therefore enemy will never be defined because a string will never equate to an integer.

To fix this, change your if and elif statements to check if enemynum is equal to 1, 2, or 3 as an integer e.g.

if enemynum == 1:
    enemy = GnomeIG

Upvotes: 1

Related Questions