Advertisement
HeidiHeff

Who's Yo Daddy?

Nov 12th, 2012
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. # who_is_your_daddy.py
  2. # Who's Yo Daddy?
  3. #
  4. # User can enter name of a male and produce the name of his father.
  5. # User is able to add, replace, and delete son-father pairs.
  6. #
  7. # Heidi Heffelfinger
  8. # November 12, 2012
  9. # Programming Python for the Absolute Beginner 3rd Ed. Chapter 5
  10. # Challenge 3
  11.  
  12. # welcome user and explain game
  13. print(
  14. """
  15.    Welcome to Who's Yo Daddy!
  16.    
  17.    This program allows you to find fathers of particular sons.
  18.    You can also add, replace, or delete father-son pairs.
  19.  
  20.    Have fun!
  21. """
  22. )
  23.  
  24. # create dictionary
  25. paternal_pairs ={"Hans Johnson": "Zacharias Johnson",
  26.                  "Alexander Melville Bell": "Alexander Graham Bell",
  27.                  "Sakichi Toyoda": "Kiichiro Toyoda",
  28.                  "Wilbert Gore": "Robert Gore",
  29.                  "Mark Chamberlain": "Alex Oxlade-Chamberlain",
  30.                  "Johan Cruyff": "Jordi Cruyff",
  31.                  "Paul Ince": "Thomas Ince",
  32.                  "Zinedine Zidane": "Enzo Zidane",
  33.                  "Kirk Douglas": "Michael Douglas",
  34.                  "Donald Sutherland": "Kiefer Sutherland"}
  35.  
  36. # initialize variables
  37. choice = None
  38.  
  39. # program
  40. while choice != 0:
  41.     print(
  42.     """
  43.    Who's Yo Daddy:
  44.  
  45.    0 - Exit
  46.    1 - Look Up a Son of a Father
  47.    2 - Add Father-Son Pair
  48.    3 - Remove Father-Son Pair
  49.    4 - Replace Son in Father-Son Pair
  50.    5 - Replace Father in Father-Son Pair
  51.    6 - Replace Father and Son in Father-Son Pair
  52.    7 - List All Father-Son Pairs
  53.    """
  54.     )
  55.  
  56.     choice = input("What would you like to do?: ")
  57.     print()
  58.  
  59.     # exit
  60.     if choice == "0":
  61.         print("Goodbye")
  62.         break
  63.  
  64.     # look up father-son pair
  65.     elif choice == "1":
  66.         father = input("What is the father's name?: ")
  67.         if father in paternal_pairs:
  68.             son = paternal_pairs[father]
  69.             print("\nThe son of", father, "is", son)
  70.         else:
  71.             print("Sorry, that father is not listed. Try adding "\
  72.                   "a father-son pair.")
  73.  
  74.     # add a father-son pair
  75.     elif choice == "2":
  76.         father = input("What is the full name of the father you wish to add? ")
  77.         if father not in paternal_pairs:
  78.             son = input("\nWhat is the full name of the son?: ")
  79.             paternal_pairs[father] = son
  80.             print("\n", father, "with son,", son, "has been added.")
  81.         else:
  82.             print("\nThat father is already in the list. Try replacing.")
  83.  
  84.     # remove father-son pair
  85.     elif choice == "3":
  86.         father = input("What is the full name of the father for the pair "\
  87.                        "you wish to delete?: ")
  88.         if father in paternal_pairs:
  89.             del paternal_pairs[father]
  90.             print("\nOK, the pair has been deleted.")
  91.         else:
  92.             print("\nI cannot do that!", father, "doesn't exist in list.")
  93.  
  94.     # replace son in father-son pair
  95.     elif choice == "4":
  96.         father = input("What is the full name of the father for the pair "\
  97.                        "you wish to replace the son of?: ")
  98.         if father in paternal_pairs:
  99.             son = input("\nWhat is the full name of the son?: ")
  100.             paternal_pairs[father] = son
  101.             print("\nOK,", son, "has been added to", father)
  102.         else:
  103.             print("\nThat father doesn't exist in list! Try adding him.")
  104.  
  105.     # replace father in father-son pair
  106.     elif choice == "5":
  107.         father = input("What is the full name of the father for the pair "\
  108.                        "you wish to replace the father in?: ")
  109.         if father in paternal_pairs:
  110.             new_father = input("\nWhat is the full name of the new father?: ")
  111.             paternal_pairs[new_father] = paternal_pairs[father]
  112.             print("\nOK,", father, "has been replaced with", new_father)
  113.             del paternal_pairs[father]
  114.         else:
  115.             print("\nThat father doesn't exist in list! Try adding him.")
  116.  
  117.     # replace entire father-son pair
  118.     elif choice == "6":
  119.         father = input("What is the full name of the father for the pair "\
  120.                        "you wish to replace?: ")
  121.         if father in paternal_pairs:
  122.             new_father = input("\nWhat is the full name of the new father?: ")
  123.             son = input("What is the full name of the new son?: ")
  124.             paternal_pairs[new_father] = son
  125.             print("\nOK,", father, "and his son,", paternal_pairs[father],\
  126.                   "have been replaced with", new_father, "and his son,",\
  127.                   paternal_pairs[new_father])
  128.             del paternal_pairs[father]
  129.         else:
  130.             print("\nThat father doesn't exist in list! Try adding him.")
  131.  
  132.     # list all father-son pairs
  133.     elif choice == "7":
  134.         for father, son in paternal_pairs.items():
  135.             print(father, "and his son,", son)        
  136.  
  137.     # some unknown choice
  138.     else:
  139.         print("\nSorry, but", choice, "isn't a valid choice.")
  140.  
  141. # wait for user
  142. input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement