0

I'm 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 later create 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 my paranthesies are ok. Thanks for your help!

  • 4
    class is a reserved python keyword, that's why it's not working :) try with `self.class_name` or something else – rednaks Jan 30 '20 at 14:43
  • 2
    Don't do IO inside `Hero.__init__`; both the name and the class should be passed as arguments instead. – chepner Jan 30 '20 at 14:46

2 Answers2

1

The word class is a reserved one, it belongs to the language, you can only use it for declaring classes.

marcos
  • 4,473
  • 1
  • 10
  • 24
0

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

rednaks
  • 1,982
  • 1
  • 17
  • 23