Search code examples
pythonfunctioninputcalculatorstrftime

i have a problem with my age calculator in python


I want the code to print the person age and the day he born, in this format (you're 31 years old and you have born in friday) but i have a problem in the code that say(Parameter 'fmt' unfilled: 11)

The Code:

import datetime


def ma(birthdate):
    today = datetime.date.today()
    age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)), birthdate.strftime('%A')
    return age


print('You are', ma(datetime.date(int(input('Please Enter a Year: ')), int(input('Please Enter a Month: ')),
                                  int(input('Please Enter a Day: ')))), 'years old, and you have born in', datetime.date.strftime('%A'))```

Output:

Please Enter a Year: 2007
Please Enter a Month: 8
Please Enter a Day: 17
Traceback (most recent call last):
  File "C:\Users\walee\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\age calculate.py", line 11, in <module>
    int(input('Please Enter a Day: ')))), 'years old, and you have born in', datetime.date.strftime('%A'))
TypeError: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'str' object

Process finished with exit code 1```

Solution

  • Simple is better than complex. (The Zen of Python)

    age = ma(datetime.date(int(input('Please Enter a Year: ')),
                           int(input('Please Enter a Month: ')),
                           int(input('Please Enter a Day: '))))
    
    print(f'You are {age[0]} years old and you were born on {age[1]}')
    

    Output:

    Please Enter a Year: 2007
    Please Enter a Month: 8
    Please Enter a Day: 17
    You are 14 years old and you were born on Friday