def printdash():
print('-' * 10)
# creates a mapping of state to abbreviation
states = {
'Oregon': 'OR',
'Florida': 'FL',
'California': 'CA',
'New York': 'NY',
'Michigan': 'MI'
}
# creates a basic set of states with some cities in them
cities = {
'CA': 'Sacramento',
'MI': 'Lansing',
'FL': 'Tallahasee'}
# add some more cities to the list
cities['NY'] = 'Albany'
cities['OR'] = 'Eugene'
# Print out some cities
printdash()
print('New York State has: ', cities['NY'])
print('Oregon has: ', cities['OR'])
# print some states
printdash()
print 'Michigan\'s abbreviation is: ' , states['Michigan']
print 'Florida\'s abbreviation is: ', states['Florida']
# do it by using the state then cities dict. Nested dicts!
printdash()
print 'Michigan has: ', cities[states['Michigan']]
print 'Florifa has: ', cities[states['Florida']]
# print every states abbreviation
printdash()
for states, abbrev in states.items():
print '%s is abbreviated as %s' % (states, abbrev)
# end
# print every city in each state
printdash()
for abbrev, cities in cities.items():
print '%s has the city %s' % (abbrev, cities)
# end
# doing both at the same time
printdash()
for state, abbrev in states.items():
print '%s state is abbreviated %s and has city %s' % (state, abbrev, cities[abbrev])
每次我运行它时,它将到达第54行(最后一个for循环),然后提升属性错误标志。在我的一生中,我不知道我做错了什么,因为其他两个环,建立在几乎相同的时尚工作没有问题。
在这个网站上查找其他解决方案时,我发现以前的示例比我能理解的要复杂一些,而且解决方案似乎比这个非常一般的情况更具体一些。
尚克斯!
目前没有回答
相关问题 更多 >
编程相关推荐