【发布时间】:2017-10-31 22:15:31
【问题描述】:
我对我的代码进行了三次检查,据我所知,它与书中给出的 ex40 基本相同(LPTHW,Python 2.7):
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
city_found = cities['_find'](cities, state)
print city_found
预期的输出是这样的:
State? (ENTER to quit) > CA
San Francisco
State? (ENTER to quit) > FL
Jacksonville
State? (ENTER to quit) > O
Not found.
State? (ENTER to quit) > OR
Portland
State? (ENTER to quit) > VT
Not found.
State? (ENTER to quit) >
但是当我运行它时,输入行只是重复,直到我在空白行上按回车,从不产生城市。像这样:
State? (Enter to quit) > KS
State? (Enter to quit) > NY
State? (Enter to quit) > LA
State? (Enter to quit) > CA
State? (Enter to quit) >
Not found.
谁能告诉我我忽略了什么?
谢谢。
【问题讨论】:
标签: python python-2.7 dictionary key-value