Question

So i am making a text adventure game, and currently i am making the enemies. My class random_enemies makes trash mobs for your character to fight and i have a function in it called weak, normal, strong, etc... that scales with your character depending on which one it is. When i call random_enemies.weak it says (Name Error: global variable "p" is not defined) even though it should be.

import random
from character import *
from player import *

class random_enemies(character):
    def __init__(self,name,hp,maxhp,attack_damage,ability_power,exp):
        super(random_enemies,self).__init__(name,hp,maxhp)
        self.attack_damage = attack_damage
        self.ability_power = ability_power
        self.exp = exp
    def weak():
        self.hp = random.randint(p.maxhp/10, p.maxhp/5)
        self.attack_damage = None
        self.ability_power = None
        self.exp = None



from character import*
class player(character):
    def __init__(self,name,hp,maxhp,attack_damage,ability_power):
        super(player,self).__init__(name, hp, maxhp)
        self.attack_damage = attack_damage
        self.ability_power = ability_power

This is my player class and below is the class that player gets "maxhp" from.

class character(object):
    def __init__(self,name,hp,maxhp):
        self.name = name
        self.hp = hp
        self.maxhp = maxhp
    def attack(self,other):
        pass



p=player(Players_name, 100, 100, 10, 5,)
while (p.hp>0):
    a=input("What do you want to do?")
    if a=="Instructions":
        Instructions()
    elif a=="Commands":
        Commands()
    elif a=="Fight":
        print("Level",wave,"Wave Begins")
        if wave < 6:
            b = random_enemies.weak()
            print("A",b,"Appeared!")
            print("Stats of",b, ": \n Health=", b.hp,"Attack Damage=",b.attack_damage)
            continue

I just made this really quickly just to test if everything I had was working until I got the error. This is also the place where random_enemies.weak() was called. Also in this is where I defined what "p" was.

Was it helpful?

Solution

So, first of all, follow a naming convention. For python code I recommend that you use pep8 as a convention.

You have a problem with classes vs. instances in your code. First, you need an instance of a class before you can use it:

enemy = random_enemy()  # a better name would be RandomEnemy

In Python, all methods start with self, and you need to pass to the method the arguments that it needs to do its work. weak is a method, so it should be more like this:

def weak(self, player):
   # the method for weak ... weak attack ?
   # remember to change p to player, which is more meaningful
   ...

Now that you have your instance and it has a method weak which receives a player as argument, you can use it as follows:

# you can't use random_enemy here as you tried because it is a class
# you need a random_enemy instance, the enemy that I talked about earlier
b = enemy.weak(player)  # renamed p to player because it is more meaningful

For this all to work, you will need one more thing. weak() needs to return something. Right now you are using what it returns, nothing! The code that you posted is b = random_enemies.weak(). Because weak() does not have a return clause, b will always be None.

Some notes: Avoid one-letter variables unless there is a long standing convention (like using i for loop counter). It is easier to understand what you are trying to do if you define player instead of just p.

Python has a really great tutorial for all this stuff.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top