Home > Software design >  How do i make this code more compact? (python)
How do i make this code more compact? (python)

Time:12-16

birth_year = int(input("What year were you born: "))
current_year = int(input("What year is it: "))
name = input("What is your name: ")
age = current_year-birth_year
print("Your name is", name , "and you are", age , "years old.")

#asks for your birth year, current year and your name then it calculates your age and prints your age and name

CodePudding user response:

Code is fine! But if you want to improve it I would recommend one thing - don't ask about current year as it can be easily taken using datetime module

from datetime import date

current_year = date.today().year

CodePudding user response:

While

print("Your name is", input("What is your name: "), "and you are", int(input("What year is it: ")) - int(input("What year were you born: ")), "years old.")

would be more compact, it's no longer easy to read. It's better to leave it the way it is now.

Edit: You should consider calculating not only the year but also the date. You might want to have a look at the python datetime module.

  • Related