#create a mapping of state to abbreviation
states = [
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
]
# create a basic set of states and some cities in them
cities = [
'CA': 'San Francisco',
'MI': 'Detroit',
'FL': 'Jacksonville'
]
,
#add someor more cities
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
#rprint out some citis
print '-' * 10
print "NY state has: ", cities['NY']
print "OR state has: ", cities['OR']
#print some states
print '-' * 10
print "Michigan's abbreviation is: ", states['Michigan']
print "Florida's abbreviation is: ", states['Florida']
在ex39_dictionaries.py中存储此代码。当我执行它时,我看到下面的错误。这段代码怎么了?
文件“ex39_dictionaries.py”,第3行 '俄勒冈':'或', ^ SyntaxError:语法无效
答案 0 :(得分:1)
states = [
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
]
使用方括号,这是尝试定义列表,列表项必须用逗号分隔。但是,您似乎想在此处创建字典(即key: value
对的集合),并且必须使用大括号(大括号):
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}