J'utilise python 2.7.4 sur une machine Linux. Mon guide est le livre « Apprendre Python à la dure » et je suis à l'exercice 39ème et voici mon code:Erreur de syntaxe Python lors de la définition d'un mappage
# states and their abberavation
states = [
'Bihar' : 'BIH'
'Jharkhand' : 'JK'
'Bengal' : 'BEN'
'Tamilnadu' : 'TN'
'Haryana' : 'HY'
'Kerla' : 'KER'
]
# states with their cities
cities = [
'BIH' : 'Patna'
'JK' : 'Ranchi'
'BEN' : 'Kolkatta'
]
# add some more cities
cities['CHN'] = 'Chennai'
cities['BWN'] = 'Bhiwani'
#print out some cities
print '-' * 10
print "TN State has:", cities['CHN']
print "HY State has:", cities['BWN']
# print some states
print '-' * 10
print "Kerla's abbreviation is :", states['Kerla']
print "Jharkhand's abbreviation is:", states['Jharkhand']
# do it by using the state then cities dict
print '-' * 10
print "Bihar has:", cities[states['Bihar']]
print "Bengal has", cities[states['Bengal']]
# print every state abbreviation
print '-' * 10
for state, abbrev in states.items():
print "%s is abbreviated %s" % (state, abbrev)
# print every city in state
print '-' * 10
for abbrev, city in cities.items():
print "%s has the city %s" % (abbrev, city)
# now do both at the same time
print '-' * 10
for state, abbrev in states.items():
print "%s state is abbreviated %s and has city %s" % (state, abbrev, cities[abbrev])
print '-' * 10
#safely get an abbreviation by state that might not be there
state = states.get('Maharashtra', None)
if not state:
print "Sorry, no Maharashtra."
#get a city with a default value
city = cities.get('MH' 'Does Not Exist')
print "The city for the state 'MH' is: %s" % city
et l'erreur que je reçois est tout simplement,
File "ex39.py", line 3
'Bihar' : 'BIH'
^
SyntaxError: invalid syntax
J'ai essayé coller copie le code exact mais je reçois toujours la même erreur. Comment ce colon est-il responsable de l'erreur? Toute aide sera très appréciée.
Utilisez {{}} 'pour les dictionnaires. '[..]' est une * liste *. –
https://docs.python.org/2/library/stdtypes.html Conférence pour vous – matiit