05-07-2024, Saat: 16:30
(Son Düzenleme: 15-07-2024, Saat: 15:17, Düzenleyen: mike983293.)
Hello everyone,
I'm working on a simple age calculator program but I'm having some trouble getting the correct age output. The program should take the user's birth date and calculate their current age based on today's date. However, the age calculation is sometimes off by a year, especially if the birthday has not occurred yet this year.
Here is a piece of my code:
Python
Copy code
Can anyone see where I'm going wrong? Is there a better way to calculate age so it's always correct?
Thanks in advance for your help!
I'm working on a simple age calculator program but I'm having some trouble getting the correct age output. The program should take the user's birth date and calculate their current age based on today's date. However, the age calculation is sometimes off by a year, especially if the birthday has not occurred yet this year.
Here is a piece of my code:
Python
Copy code
Kod: (Select All)
from datetime import datetime
def calculate_age(birthdate):
today = datetime.today()
age = today.year - birthdate.year
if today.month < birthdate.month or (today.month == birthdate.month and today.day < birthdate.day):
age -= 1
return age
birthdate = datetime(1990, 7, 5)
print("Yaş:", calculate_age(birthdate))
Thanks in advance for your help!