0

I'm working on a dungeon styled game, and I made a file called places.py to store the places. I am using a class, then adding attributes to the class. I received an error, and here is the error message:

E0001:invalid syntax (, line 8)

Here would be my code for places.py:

class place(object):
    def __init__(self):
        self.name=''
        self.nearplaces=[]
        self.ground=[]
        self.monsters=[]
1 = place
1.name='Ruby City'
1.nearplaces=[2,3]
1.ground=[1]
1.monsters=[]

It is confusing how, after declaration, I receive an error when trying to change the attributes. This may be a very simple question, but I wish to know why it does not work, and how to make it work. Thank you.

3
  • Variable names cannot begin with an integer. Apart from that you are not creating an instance, you assign the class. Commented Feb 27, 2018 at 12:00
  • They cannot? Why can't they? Can you please give me a link? Commented Feb 27, 2018 at 12:01
  • They just... can't (@Phydeaux's answer below links the documentation stating that they can't.). This is intuitive if you consider how a parser actually works - consider the example print(3e2). Is that trying to print the number 30.0 or the variable 3e2? Commented Feb 27, 2018 at 12:05

1 Answer 1

1

Firstly you cannot use integer literals, like 1, as variable names. This is to avoid ambiguity, so the interpreter knows whether it is looking at a variable or a number (see rules for Python identifiers).

Secondly, to properly instantiate your class you need to use ().

Try:

level_1 = place()
level_1.name='Ruby City'
level_1.nearplaces=[2,3]
level_1.ground=[1]
level_1.monsters=[]
Sign up to request clarification or add additional context in comments.

Comments

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.