我正在閱讀zed shaw的書「學習python的困難之路」。原諒我,但我是一個編程新手,我很難理解這一點。我似乎無法看到find_city函數如何通過輸入狀態來找出要返回的城市。 !與「好講究」和」#這條線是最重要的有史以來行研究是那些困惑我PYTHON代碼從一本書的教訓
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."
# ok pay attention!
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
# this line is the most important ever! study!
city_found = cities['_find'](cities, state)
print city_found
*什麼特別*你懂不?更新你的問題來解釋。 –
正如@ChrisMorgan所說,你應該編輯你的問題,說明你想要解釋的具體部分,因爲人們不知道該假設什麼。 – jamylak
@ChrisMorgan我已經意識到並編輯它:D – jamylak