Max Niederhofer wrote:
Hello all,
first post, please be gentle. I'm having serious trouble finding an
alternative for the deprecated find module for dictionaries.
What find module for dictionaries?
The code (from Zed Shaw's Hard Way, exercise 40) goes something like
this. Hope indentation survives.
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
cities['_find'] = find_city
What is the purpose of this?
You have a dictionary called "cities" that contains four items. The
first three are pairs of State:City, which makes sense. The fourth is a
pair of the word "_find" matched with a function find_city. I don't
understand what the purpose of this is.
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
city_found = cities['_find'](cities, state)
print city_found
I think you need to include the actual search inside the while loop.
Otherwise, the loop keeps asking for a new state, but doesn't do
anything with it until you exit the loop.
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
city_found = cities['_find'](cities, state)
print city_found
But I don't understand the purpose of placing the function inside the
dictionary. Much clearer and simpler is:
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
print find_city(cities, state)
--
Steven
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor