[Tutor] Python Hard_way 40

Robert . forums at zhub.co.uk
Wed May 11 17:24:02 CEST 2011


Hi all,

My first post ever! :)
I'm following the guide "Learn Python the Hard Way" and have reached a point
where I am struggling to understand the code and unfortunately the authors
guide hasn't cleared it up for me. (excercise 40 for reference)

The main confusion is coming from* 'cities['_find'] = find_city'* I'm not
sure what this is really doing.

>From my learning, this should add an extra item to the dict with the index
name of "_find" and the value which is returned from "find_city" function.

run I add "print cities" after the above function to try and look at how the
dict is now populated, I am still confused.

(PasteBin version to see highlighting and indents  ->
http://pastebin.com/gmngh6sc   Not sure how it will look in email)
------------------
cities = {'ca': 'San Fran','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

----------------------

Grateful for any help in explaining how this program is working and each
step it is taking.

kindest regards
Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110511/fff861b5/attachment-0001.html>


More information about the Tutor mailing list