Anki
Anki

Reputation: 29

invalid syntax (<string>)

# create a mapping of state to abbreviation
states = [
    'Oregon': 'OR',
    'Florida': 'FL',
    'California': 'CA',
    'New York': 'NY',
    'Michigan': 'MI'
    ]

I am learning Python from Learning Python The Hard Way. In example 39 of the book, I typed the same code as shown above for creating a dictionary, even copied and pasted it, but I m getting an error E0001:invalid syntax (<string>, line 3) and it is pointing at :. What went wrong?

Upvotes: 1

Views: 3742

Answers (2)

Ethan
Ethan

Reputation: 69

You want to use curly braces for dictionaries

states = {
    'Oregon'    : 'OR',
    'Florida'   : 'FL',
    'California': 'CA',
    'New York'  : 'NY',
    'Michigan'  : 'MI'
    }

Upvotes: 2

ShadowRanger
ShadowRanger

Reputation: 155363

Square brackets ([]) are used for list literals. In this case, you're supposed to be making a dict literal (the : is used to separate the key on the left of the colon from the value on the right), which is delimited by curly braces ({}).

Upvotes: 3

Related Questions