[Tutor] Urgent question about program debugging

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 6 09:48:49 CEST 2006


> father_son = {"Kirk Douglas" : "Michael Douglas",
>               "James Brolin" : "Josh Brolin",
>               "Bob Dylan" : "Jakob Dylan"}
>
>    elif choice == "2":
>        print "LIST OF ALL FATHERS\n"
>        for father_son in father_son:
>            print father_son

You are replacing the reference to the dictionary with
a single key entry here. You probably intended

for father in father_son:
   print father

>    # look up father-son pairs
>    elif choice == "3":
>        term = raw_input("Type in the name of the father of this pair: ")
>        if term in father_son:

father_son is no longer the dictionary but refers to the last father!

It should work if you don't list the fathers first! :-)

>    elif choice == "4":
>        term = raw_input("Type in the name of the father you want to add: 
> ")
>        if term not in father_son:
>            definition = raw_input("Type in the name of this person's son: 
> ")
>            father_son[term] = definition

As a wee point, its usually considered a bad idea to name variables
after their syntax meaning rather than their meaning in the program.
Thus this would be better written:

father = raw_input(...)
if father not in father_son:
    son = raw_input(...)
    father_son[father] = son

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list