Home > other >  Why does the syntax error in my dictionary point to the ":" and suggest to use a comma?
Why does the syntax error in my dictionary point to the ":" and suggest to use a comma?

Time:01-27

I'm following a tutorial where I need to write a dictionary:

from classes.game import Person, bcolors

magic = [{"name: Fire", "cost": 10, "dmg": 60},
         {"name: Thunder", "cost": 10, "dmg": 60},
         {"name: Blizzard", "cost": 10, "dmg": 60}]

player = Person(460, 65, 60, 34, magic)
enemy = Person(1200, 65, 45, 25, magic)

in line 3 there is a syntax error:

    magic = [{"name: Fire", "cost": 10, "dmg": 60},
                                  ^
SyntaxError: invalid syntax

Why does it point to the colon (:) after "cost"? Pycharm says it expects a comma, however that further causes issues.

CodePudding user response:

You wrote "name: Fire", which presumably should be "name": "Fire".

CodePudding user response:

Your syntax is off:

magic = [{"name": "Fire", "cost": 10, "dmg": 60},
         {"name": "Thunder", "cost": 10, "dmg": 60},
         {"name": "Blizzard", "cost": 10, "dmg": 60}]

The syntax on a dictionary is "key": value. You had "key:value".

CodePudding user response:

You're forgetting to put quotes dividing the "name" key and values.

from classes.game import Person, bcolors

magic = [{"name": "Fire", "cost": 10, "dmg": 60},
         {"name": "Thunder", "cost": 10, "dmg": 60},
         {"name": "Blizzard", "cost": 10, "dmg": 60}]
#              ^  ^

player = Person(460, 65, 60, 34, magic)
enemy = Person(1200, 65, 45, 25, magic)

CodePudding user response:

Your mistake was that you wrote "name: Fire" instead of "name": "Fire".

Besides dictionaries, there is another kind of collection in Python that are written with {}, but without ::

names = {"Fire", "Thunder", "Blizzard"}

These are called sets.

By writing

{"name: Fire", "cost": 10, "dmg": 60}

Python (or Pycharm) thought you meant to write a set, consisting of the string "name: Fire", and then the string "cost". But then there was a : which does not occur in a set. So that's where they thought you made a mistake, even if your mistake was already earlier in that line.

  •  Tags:  
  • Related