01/10/2018, 10:41

Code Dictionaries bị lỗi SyntaxError: invalid syntax

mình đang học cuốn “learn python the hard way” bài 39:

1.# create a mapping of state to abbreviation
2.states = {
3.    'Oregon': 'OR',
4.    'Florida': 'FL',
5.    'California': 'CA',
6.    'New York': 'NY',
7.    'Michigan': 'MI',
8.}
9.
10.# create a basic set of states and some cities in them
11.cities = {
12.    'CA': 'San Francisco',
13.    'MI': 'Detroit',
14.    'FL': 'Jacksonville'
15.}
16.
17.# add some more cities
18.cities['NY'] = 'New York'
19.cities['OR'] = 'Portland'
20.
21.# print out some cities
22.print '-' * 10
23.print "NY State has: ", cities['NY']
24.print "OR State has: ", cities['OR']
25.
26.# print some states
27.print '-' * 10
28.print "Michigan's abbreviation is: ", states['Michigan']
29.print "Florida's abbreviation is: ", states['Florida']
30.
31.# do it by using the state then cities dict
32.print '-' * 10
33.print "Michigan has: ", cities[states['Michigan']]
34.print "Florida has: ", cities[states['Florida']]
35.
36.# print every state abbreviation
37.print '-' * 10
38.for state, abbrev in states.items():
39.    print "%s is abbreviated %s" % (state, abbrev)
40.
41.# print every city in state
42.print '-' * 10
43.for abbrev, city in cities.items():
44.    print "%s has the city %s" % (abbrev, city)
45.
46.# now do both at the same time
47.print '-' * 10
48.for state, abbrev in states.items():
49.    print "%s state is abbreviated %s and has city %s" % (
50.        state, abbrev, cities[abbrev])
51.
53.print '-' * 10
54.# safely get a abbreviation by state that might not be there
55.state = states.get('Texas')
56.
57.if not state:
58.    print "Sorry, no Texas."
59.
60.# get a city with a default value
61.city = cities.get('TX', 'Does Not Exist')
62.print "The city for the state 'TX' is: %s" % city

lúc run nó cứ báo lỗi:

File “ex39.py”, line 4
‘Florida’: ‘FL’
^
SyntaxError: invalid syntax

Ai biết giải thích dùm mình! Cảm ơn!

HK boy viết 12:57 ngày 01/10/2018

Bạn không cần thêm line number ở trước code đâu. Mình nhớ là đã có người nhắc bạn về vấn đề này rồi.

‘Michigan’: ‘MI’,

Dòng này thừa dấu , ở cuối?

Bài liên quan
0