This code is to have a dictionary of names and birthdays.

It will ask who's birthday it is that you're looking for. Then if it is in the dictionary, it will print back what that birthday is. If not, it will ask if you want to add it then have you insert the birthday as the value and use the name that you input before as the key. Then finally print the full dictionary.

I am pretty new to coding in Python and feel like there is a better way to type this code. Was thinking I could use return but I'm still not quite sure what the difference between return and print is. Is there a way to make this shorter or more efficient?

birthdays = {'Jon': 'July 17', 'Shauna': 'Jan 27', 'Lynette': 'July 10'}

while True:
    print("Who's birthday is it that you are looking for? To cancel, press enter!")
    name = input()
    if name == '':

    if name in birthdays:
        print(birthdays[name] + ' is the birthday of ' + name)
        print("I don't have that person's name in my archives. Would you like to add it? Press Y or N")
        answer = input()
        if answer == 'Y':
            new_name = name
            print("Please enter their birthday")
            new_birthday = input()
            birthdays[name] = new_birthday
            print('It has been added')
            print(new_name + "'s birthday has been added as " + new_birthday)
            print('Here is a list of birthdays you have saved...')
            print('Alrighty Then')
  • 1
    \$\begingroup\$ "Was thinking I could use return but I'm still not quite sure what the difference between return and print is." Huh, what please? You can use return birthdays in the context of a function which is called elsewhere and print's the result. Other function won't have access to what's printed on the screen from your code. \$\endgroup\$ Feb 27, 2020 at 17:28
  • 1
    \$\begingroup\$ @πάνταῥεῖ: While that question immediately identifies the OP as a beginner not only in Python but in programming in general, it seems to be a common enough cause of confusion, in my experience. \$\endgroup\$
    – Graipher
    Feb 28, 2020 at 16:45

2 Answers 2


return returns the value, in this case the dictionary birthdays, whereas print just prints the value to the terminal. Your printing might be improved by using f-strings. This is usually a bit easier than having to use "+" inside your print statements. So for example, instead of

print(new_name + "'s birthday has been added as " + new_birthday)

You could write:

print(f"{new_name}'s birthday has been added as {new_birthday}")

This combined with a generator could also make your dictionary printing a little neater:

print("\n".join([f"{name}'s birthday is {bday}" for name,bday in birthdays.items()]))

The generator creates a list of strings for each item in the birthdays dictionary, so in this case, it would generate the list ["Jon's birthday is July 17","Shauna's birthday is Jan 27", "Lynette's birthday is July 10"]. The join() function is used to combine each element of this list into a new string, separated by the newline character \n. So the final result would produce: Jon's birthday is July 17 Shauna's birthday is Jan 27 Lynette's birthday is July 10

If you want to further extend this code, I'd recommend representing the birthday as a date object, from the Python built-in library datetime. Then you could also add the functionality of easily telling how many days until someone's birthday and other features like that.

  • 1
    \$\begingroup\$ Ah, thank you so much! This is incredibly helpful. I didn't know that library existed. I'll have to check it out and I also didn't know f-strings were a thing. Looks like a great way to condense part of the code. I appreciate it! \$\endgroup\$
    – anhanyoung
    Feb 27, 2020 at 19:45

If this were a real program (i.e. not simply something you are playing and learning with), it would need a lot more error checking.

E.g. If this happens:

Would you like to add it? Press Y or N
Alrighty Then

The program's ambiguous response will mislead the user into thinking that his answer has been accepted, and then the user will get confused about what happens next.

It would be better to have a function that prompts the user and indicates whether a positive or negative response was entered. E.g.

if positive_response("I don't have that person's name. Would you like to add it?"):

Where positive_response(prompt) can have long lists of possible positive and negative responses and can loop until it gets a definite hit. The function can be reused later in other parts of the program that need a confirmation.

Similarly nothing checks the entered birthday (e.g. "today" or "last Wednesday"). Again, a self-contained function that loops until it gets a date that it can understand would be appropriate:

birthdays[name] = get_date("Please enter their birthday")

The function should also convert the date into a standard format. And again, the function can be reused elsewhere if a date is required from input.

This code is simpler, easier to read, and far less likely to contain bugs:

if positive_response("I don't have that person's name. Would you like to add it?"):
    birthdays[name] = get_date("Please enter their birthday")
    display_birthdays("Updated list of birthdays")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.