← Back to Community
AttributeError: 'list' object has no attribute 'current_health'
Profile icon
SamKunefeh

When I run this repl, it chooses a random class and assigns current_enemy to it. However, when I try to access the class' attributes, it gives this error:

AttributeError: 'list' object has no attribute 'current_health'

I can't seem to figure out why it's giving this error. After all, I am choosing a class, so I should be able to access it's attributes right?

Answered by dillonjoshua68 [earned 5 cycles]
View Answer
Comments
hotnewtop
Profile icon
FlaminHotValdez

You never created a Player, you only defined the class.

Profile icon
FlaminHotValdez

That means the program has no Player to get a current_health from.

Profile icon
dillonjoshua68
Profile icon
dillonjoshua68

I think he has to creat a function inside the class for the health like this... @maxyang

For example

class Player: def __init__(self, health): self.health = 100 def health(): chealth = self.health

I’m not sure if this is correct tho

Profile icon
FlaminHotValdez

@dillonjoshua68 Good point, I keep forgetting classes need the init function. I'm mostly a c++ coder, but I know code logic and a bit of Python.

Profile icon
dillonjoshua68
Profile icon
SamKunefeh

@maxyang Hi, I made the player instance and instances of the enemies and even removed the enemy list, but now it's giving me a new error which is going on the top of my head.

Profile icon
SamKunefeh

@dillonjoshua68 btw, How do I use the function?

Profile icon
dillonjoshua68
Profile icon
FlaminHotValdez

@Jackbaklava What is your bug?

Profile icon
SamKunefeh

@maxyang Don’t worry, the problem was that I was defining a variable local to the function, and then trying to access it globally. All I had to do, was just make it a global variable so that I could access it outside the function. Anyways, thanks for your help, appreciate it!!!

Profile icon
dillonjoshua68

@Jackbaklava , current_enemy refers to the list you created (enemy_list), and the program says that that list doesn’t hav an attribute called current_health