+ 3

Can anyone make a program that determines your exact age (year, month and day) in python?

I tried having two inputs, one for the date of birth and one for the current date, but because months have different numbers of days it kept being off by one or more days.

13th Apr 2017, 7:09 AM
Ontleybot
Ontleybot - avatar
2 Antworten
+ 7
You can import datetime and get the current date from there. Also, convert user input to datetime module's objects and you can easily perform adequate arithmetics on them.
13th Apr 2017, 7:59 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 20
write a function that calculates how many days are in each month. you can do this by declaring when the last leap year was and how many days were in the month after that. after that... have the function generate a sum of the days since that leap year up to this day. if leap year was two years ago for example... +29.. then if there were 31 days in the month after that.. +31 +30 +31 +30 until the present day. then have it calculate the days in between leap year and your birthday by doing the same thing.. except keep a counter to keep track of when the previous leap year is... so every 4 years that go by.. add one extra day onto the sum for that year.
13th Apr 2017, 7:59 AM
Fox
Fox - avatar