Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The class:
Python
class Race():
    def __init__(self,raceName,special_abilites = [], race_strength=0, race_dexterity=0, race_constitution=0, race_intelligence=0, race_wisdom=0, race_charisma=0, size="none", speed=0):
        self.raceName = raceName
        self.special_abilites = special_abilites
        self.race_strength = race_strength
        self.race_dexterity = race_dexterity
        self.race_constitution = race_constitution
        self.race_intelligence = race_intelligence
        self.race_wisdom = race_wisdom
        self.race_charisma = race_charisma
        self.size = size
        self.speed = speed

    def printRace(self, *args):
        print(f"{raceName}, {race_strength}, {race_dexterity}, {race_constitution}, {race_intelligence}, {race_wisdom}, {race_charisma}, {size}, {speed}".center(150))

Now I try to run this bit:
Python
Human = Race.printRace("Human","medium",30)
print(Human)

and then it gives me:
Python
Traceback (most recent call last):
  File "DnD game engine.py", line 20, in <module>
    Human = Race.printRace("Human","medium",30)
  File "DnD game engine.py", line 18, in printRace
    print(f"{raceName}, {race_strength}, {race_dexterity}, {race_constitution}, {race_intelligence}, {race_wisdom}, {race_charisma}, {size}, {speed}".center(150))
NameError: name 'raceName' is not defined


If you can find the cause it would be appreciated greatly, Thanks!

What I have tried:

Google which didn't turn up anything good, and Documentation which I couldn't find anything in either
Posted
Updated 21-Oct-22 18:29pm
Comments
CHill60 21-Oct-22 18:20pm    
You haven't defined raceName anywhere but have tried to use it.
The error message really is giving you the answer
Brennon Nevels 21-Oct-22 18:28pm    
Didn't I define it in the class?

1 solution

Look at your code:
Python
print(f"{raceName}, {race_strength}, ...
You don't reference self in the print statement, so the system is looking for a global variable called raceName

Try this:
Python
print(f"{self.raceName}, {self.race_strength}, ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900