Practice Python

Beginner Python exercises

03 February 2017

Birthday Dictionaries Solutions

Exercise 33

This exercise is Part 1 of 4 of the birthday data exercise series. The other exercises are: Part 2, Part 3, and Part 4.

For this exercise, we will keep track of when our friend’s birthdays are, and be able to find that information based on their name. Create a dictionary (in your file) of names and birthdays. When you run your program it should ask the user to enter a name, and return the birthday of that person back to them. The interaction should look something like this:

>>> Welcome to the birthday dictionary. We know the birthdays of:
Albert Einstein
Benjamin Franklin
Ada Lovelace
>>> Who's birthday do you want to look up?
Benjamin Franklin
>>> Benjamin Franklin's birthday is 01/17/1706.

Sample solution

One reader does the basics:

if __name__ == '__main__':
birthdays = {
'Albert Einstein': '03/14/1879',
'Benjamin Franklin': '01/17/1706',
'Ada Lovelace': '12/10/1815',
'Donald Trump': '06/14/1946',
'Rowan Atkinson': '01/6/1955'}
print('Welcome to the birthday dictionary. We know the birthdays of:')
for name in birthdays:
print(name)
print('Who\'s birthday do you want to look up?')
name = input()
if name in birthdays:
print('{}\'s birthday is {}.'.format(name, birthdays[name]))
else:
print('Sadly, we don\'t have {}\'s birthday.'.format(name))
view raw task33.py hosted with ❤ by GitHub

And here is another reader submission:

import time
Birthdays ={
"Albert Einstein": "14/3/1889",
"Bill Gates": "28/10/1955",
"Steve Jobs": "24/2/1955",
}
print("Welcome to the Birthday game ! We have the birthdays to:")
time.sleep(1)
for x in Birthdays:
print(x)
time.sleep(0.7)
choice= input("\nWho's birthday do you want to look up?")
if choice in Birthdays:
print("The birthday of {} is: ".format(choice))
print(Birthdays[choice])

Enjoying Practice Python?


Explore Yubico
Explore Yubico