Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am programming a python game, and my Hero class starts like this:

class Hero():

    def __init__(self, name):

        self.class = ""

        while self.class not in ["mage", "fighter"]:

            print("What class are you?")

            self.class = input("Mage or Fighter: ").lower()

        self.name = name.title()

        self.level = 1

        self.enimieskilled = 0

        self.hp = self.level * 5

        self.attacks = spells[self.class]

I have later created a hero with player = Hero(input("What is your name? "))

But when I run this program, I get:

File "app.py", line 26

    self.class = ""

             ^

SyntaxError: invalid syntax

I tried commenting out all the self.class's, as so:

class Hero():

    def __init__(self, name):

        #self.class = ""

        #while self.class not in ["mage", "fighter"]:

        #    print("What class are you?")

        #    self.class = input("Mage or Fighter: ").lower()

        self.name = name.title()

        self.level = 1

        self.enimieskilled = 0

        self.hp = self.level * 5

        #self.attacks = spells[self.class]

and it runs. I have checked and all of the parenthesis are ok.

1 Answer

0 votes
by (36.8k points)
edited by

Class is the reserved python keyword, that's why it's not working :) try with self.class_name or something else

Learn Python for Data Science Course to improve your technical knowledge.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...