[Tutor] Urgent question about program debugging

Charles Bartley blackspeckpotato at frontiernet.net
Thu Apr 6 03:46:48 CEST 2006


Hi,

I'm a Python newbie, but I went over the code, and I think the problem is in choice number 2.


###
        for father_son in father_son:
            print father_son
###

father_son becomes a string here.

Regards,
Charles B.

  ----- Original Message ----- 
  From: Ros Daniel 
  To: tutor at python.org 
  Sent: Wednesday, April 05, 2006 5:22 PM
  Subject: [Tutor] Urgent question about program debugging


  I just created a "who's your daddy?" program--one of the challenges in the michael dawson book.

  It was working well, and I tested all the different instructions, and then for some reason, it stopped working. Specifically, whenever I enter the name of a father, it tells me it doesn't exist, although it does, because when you list all the fathers, you can see it exists, and I set it up in the dictionary.

  It may be that I've missed something. I know I saved this program so many times, maybe I changed something without realising it, although I've been through every line of code numerous times. I don't know how to use the IDLE debugger, which could be part of my problem. The help file doesn't seem to help.

  Here's the code:



  # This program lets the user enter the first and last name of a male
  # and produces the name of his son. The user will also be able
  # to add, replace, and delete son-father pairs.

  father_son = {"Kirk Douglas" : "Michael Douglas",
                 "James Brolin" : "Josh Brolin",
                 "Marlon Brando" : "Christian Brando",
                 "George Best" : "Calum Best",
                 "David Beckham" : "Brooklyn Beckham",
                 "Bob Dylan" : "Jakob Dylan"}

  choice = None
  while choice != "0":

      print \
      """

        Welcome to Who's Your Daddy? A mini database of fathers and sons.
        When entering names, please use both the first name and the last name.
        
            0 - Exit
            1 - List all Father-Son Pairs
            2 - List all Fathers
            3 - Look Up Father-Son Pairs
            4 - Add a Father-Son Pair
            5 - Delete a Father-Son Pair
            6 - Replace the Son of a Father-Son Pair
            7 - Replace a Father-Son Pair
       """

      choice = raw_input("Choice: ")
      print

      # exit program
      if choice == "0":
          print "\nGoodbye."

      # list all father-son pairs
      elif choice == "1":
          print "\nLIST OF ALL FATHER-SON PAIRS\n"
          print father_son

      # list all fathers 
      elif choice == "2":
          print "LIST OF ALL FATHERS\n"
          for father_son in father_son:
              print father_son
          
      # 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:
              definition = father_son[term]
              print term, "is the father of", definition
          else:
              print "\nSorry,", term, "doesn't exist."

      # add a father-son pair
      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
              print "\nThank you. Your new father-son pair has been added."
              print term, "is the father of", definition
          else:
              print term, "already exists in the database. Try updating the entry."

      # delete a father-son pair
      elif choice == "5":
          term = raw_input("Type in the name of the father which corresponds \
          to the father-son pair you want to delete: ")
          if term in father_son:
              definition = father_son[term]
              del father_son[term]
              print term, "and his son", definition, "have been removed from the database."
          else:
              print "\nI don't recognise this name. Try adding it as an entry."
              
      # replace the son of a father-son pair
      elif choice == "6":
          term = raw_input("Type in the name of the father whose son you want \
          to replace: ")
          if term in father_son:
              definition = raw_input("Type in the name of this person's son: ")
              father_son[term] = definition
              print "\nThank you. Your change has been made.", term, "is now \
              the father of", definition
          else:
              print "\nSorry, that name doesn't exist."

      # replace a father-son pair
      elif choice == "7":
          term = raw_input("Type in the name of the father which corresponds \
          to the father-son pair you want to replace: ")
          if term in father_son:
              term = raw_input("Type in name of this 'replacement' father: ")
              definition = raw_input("Type in the name of this person's son: ")
              father_son[term] = definition
              print "A new father-son pair has been added.", term, "is the \
              father of", definition
      else:
          print "\nSorry, but", choice, "isn't a valid choice."

  raw_input("\n\nPress the enter key to exit")
          
          
         






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


  _______________________________________________
  Tutor maillist  -  Tutor at python.org
  http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060405/94ce10e6/attachment.html 


More information about the Tutor mailing list