0

I created an enemy class that reads data from a file to get its instance variables. The file is written to different arrays for the different types of enemies. They work fine. When I write them to the object then try to print them I get the error:

AttributeError: 'NoneType' object has no attribute 'type'

If I don't print the 'type' attribute then it says:

AttributeError: 'NoneType' object has no attribute 'health'

The class is:

def Enemy(object):
    def __init__(self, TypeStats):
        self.type = TypeStats[0][0]
        self.health = int(TypeStats[0][1])
        self.strength = int(TypeStats[0][2])
        self.dead = False

The code to write the array to the object and print the variables is:

Bandit = Enemy(BanditStats)
print Bandit.type, Bandit.health, Bandit.strength 

The reason I write it as a 2D array is because when I write the data from the file to the array it creates an array:

BanditStats = [['Bandit', '80','7']]

I don't know why that happens is it easy to fix?

1 Answer 1

2

You don't define classes using def. It should be class:

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

2 Comments

Thank you so much, I did that a second time with another class just now.
When I was on it said I had to wait a few more minutes to accept it.

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.