|
| 1 | +''' |
| 2 | +Exercise 33: Birthday Dictionaries |
| 3 | +
|
| 4 | +This exercise is Part 1 of 4 of the birthday data exercise series. |
| 5 | +The other exercises are: Part 2, Part 3, and Part 4. |
| 6 | +
|
| 7 | +For this exercise, we will keep track of when our friend’s birthdays |
| 8 | +are, and be able to find that information based on their name. |
| 9 | +Create a dictionary (in your file) of names and birthdays. When you |
| 10 | +run your program it should ask the user to enter a name, and return |
| 11 | +the birthday of that person back to them. The interaction should |
| 12 | +look something like this: |
| 13 | +
|
| 14 | +>>> Welcome to the birthday dictionary. We know the birthdays of: |
| 15 | +Albert Einstein |
| 16 | +Benjamin Franklin |
| 17 | +Ada Lovelace |
| 18 | +>>> Who's birthday do you want to look up? |
| 19 | +Benjamin Franklin |
| 20 | +>>> Benjamin Franklin's birthday is 01/17/1706. |
| 21 | +Happy coding! |
| 22 | +
|
| 23 | +''' |
| 24 | +# Solution |
| 25 | +def show_all_in_dict(dict): |
| 26 | + """ |
| 27 | + Print all the key in dict. |
| 28 | + |
| 29 | + Arguments: |
| 30 | + dict -- a dictionary needed to be print it's key. |
| 31 | + |
| 32 | + """ |
| 33 | + print('We know the birthdays of:') |
| 34 | + for key in dict: |
| 35 | + print(key) |
| 36 | + |
| 37 | +# Useless |
| 38 | +def search(query, dict): |
| 39 | + """ |
| 40 | + Search the person's birthday is in the dict or not. |
| 41 | + |
| 42 | + Arguments: |
| 43 | + query -- the name of person the user want to know. |
| 44 | + dict -- a dictionary needed to be print it's key. |
| 45 | + |
| 46 | + Returns: |
| 47 | + birthday -- the birthday of the query. |
| 48 | + """ |
| 49 | + return dict[query] if query in dict else None |
| 50 | + |
| 51 | +def main(): |
| 52 | + Birthdays ={"Albert Einstein": "14/3/1889", |
| 53 | + "Bill Gates": "28/10/1955", |
| 54 | + "Steve Jobs": "24/2/1955"} |
| 55 | + print('Welcome to the birthday dictionary.') |
| 56 | + show_all_in_dict(Birthdays) |
| 57 | + query = input("Who's birthday do you want to look up?") |
| 58 | + result = Birthdays[query] if query in Birthdays else None |
| 59 | + if result == None: |
| 60 | + print('No Data') |
| 61 | + else: |
| 62 | + print("{}'s birthday is {}".format(query, Birthdays[query])) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + main() |
| 67 | + |
| 68 | +# Test Part |
| 69 | +# >>> %Run test.py |
| 70 | +# Welcome to the birthday dictionary. |
| 71 | +# We know the birthdays of: |
| 72 | +# Albert Einstein |
| 73 | +# Bill Gates |
| 74 | +# Steve Jobs |
| 75 | +# Who's birthday do you want to look up?Bill Gates |
| 76 | +# Bill Gates's birthday is 28/10/1955 |
| 77 | +# >>> %Run test.py |
| 78 | +# Welcome to the birthday dictionary. |
| 79 | +# We know the birthdays of: |
| 80 | +# Albert Einstein |
| 81 | +# Bill Gates |
| 82 | +# Steve Jobs |
| 83 | +# Who's birthday do you want to look up?Soi |
| 84 | +# No Data |
0 commit comments