nachdem hier mehrfach auf "Learn Python The Hard Way" hingewiesen wurde, habe ich mich näher mit diese Lektüre beschäftigt und habe ein Verständnisproblem mit folgendem Script:
Code: Alles auswählen
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
Mal davon abgesehen, dass ich das vermutlich falsch verstanden habe, stellt sich für mich auch die Frage, wo jetzt die Vorteile zu dem folgenden Sprict bestehen:
Code: Alles auswählen
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(state):
try:
return cities[state]
except:
return "Not found!"
while True:
print "State? (Enter to quit)",
state = raw_input("> ")
if not state: break
print find_city(state)
Klaus