Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # who_is_your_daddy.py
- # Who's Yo Daddy?
- #
- # User can enter name of a male and produce the name of his father.
- # User is able to add, replace, and delete son-father pairs.
- #
- # Heidi Heffelfinger
- # November 12, 2012
- # Programming Python for the Absolute Beginner 3rd Ed. Chapter 5
- # Challenge 3
- # welcome user and explain game
- print(
- """
- Welcome to Who's Yo Daddy!
- This program allows you to find fathers of particular sons.
- You can also add, replace, or delete father-son pairs.
- Have fun!
- """
- )
- # create dictionary
- paternal_pairs ={"Hans Johnson": "Zacharias Johnson",
- "Alexander Melville Bell": "Alexander Graham Bell",
- "Sakichi Toyoda": "Kiichiro Toyoda",
- "Wilbert Gore": "Robert Gore",
- "Mark Chamberlain": "Alex Oxlade-Chamberlain",
- "Johan Cruyff": "Jordi Cruyff",
- "Paul Ince": "Thomas Ince",
- "Zinedine Zidane": "Enzo Zidane",
- "Kirk Douglas": "Michael Douglas",
- "Donald Sutherland": "Kiefer Sutherland"}
- # initialize variables
- choice = None
- # program
- while choice != 0:
- print(
- """
- Who's Yo Daddy:
- 0 - Exit
- 1 - Look Up a Son of a Father
- 2 - Add Father-Son Pair
- 3 - Remove Father-Son Pair
- 4 - Replace Son in Father-Son Pair
- 5 - Replace Father in Father-Son Pair
- 6 - Replace Father and Son in Father-Son Pair
- 7 - List All Father-Son Pairs
- """
- )
- choice = input("What would you like to do?: ")
- print()
- # exit
- if choice == "0":
- print("Goodbye")
- break
- # look up father-son pair
- elif choice == "1":
- father = input("What is the father's name?: ")
- if father in paternal_pairs:
- son = paternal_pairs[father]
- print("\nThe son of", father, "is", son)
- else:
- print("Sorry, that father is not listed. Try adding "\
- "a father-son pair.")
- # add a father-son pair
- elif choice == "2":
- father = input("What is the full name of the father you wish to add? ")
- if father not in paternal_pairs:
- son = input("\nWhat is the full name of the son?: ")
- paternal_pairs[father] = son
- print("\n", father, "with son,", son, "has been added.")
- else:
- print("\nThat father is already in the list. Try replacing.")
- # remove father-son pair
- elif choice == "3":
- father = input("What is the full name of the father for the pair "\
- "you wish to delete?: ")
- if father in paternal_pairs:
- del paternal_pairs[father]
- print("\nOK, the pair has been deleted.")
- else:
- print("\nI cannot do that!", father, "doesn't exist in list.")
- # replace son in father-son pair
- elif choice == "4":
- father = input("What is the full name of the father for the pair "\
- "you wish to replace the son of?: ")
- if father in paternal_pairs:
- son = input("\nWhat is the full name of the son?: ")
- paternal_pairs[father] = son
- print("\nOK,", son, "has been added to", father)
- else:
- print("\nThat father doesn't exist in list! Try adding him.")
- # replace father in father-son pair
- elif choice == "5":
- father = input("What is the full name of the father for the pair "\
- "you wish to replace the father in?: ")
- if father in paternal_pairs:
- new_father = input("\nWhat is the full name of the new father?: ")
- paternal_pairs[new_father] = paternal_pairs[father]
- print("\nOK,", father, "has been replaced with", new_father)
- del paternal_pairs[father]
- else:
- print("\nThat father doesn't exist in list! Try adding him.")
- # replace entire father-son pair
- elif choice == "6":
- father = input("What is the full name of the father for the pair "\
- "you wish to replace?: ")
- if father in paternal_pairs:
- new_father = input("\nWhat is the full name of the new father?: ")
- son = input("What is the full name of the new son?: ")
- paternal_pairs[new_father] = son
- print("\nOK,", father, "and his son,", paternal_pairs[father],\
- "have been replaced with", new_father, "and his son,",\
- paternal_pairs[new_father])
- del paternal_pairs[father]
- else:
- print("\nThat father doesn't exist in list! Try adding him.")
- # list all father-son pairs
- elif choice == "7":
- for father, son in paternal_pairs.items():
- print(father, "and his son,", son)
- # some unknown choice
- else:
- print("\nSorry, but", choice, "isn't a valid choice.")
- # wait for user
- input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement