我在 python 中的年龄计算器有问题
python 343
原文标题 :i have a problem with my age calculator in python
我希望代码以这种格式打印人的年龄和他出生的日期(你 31 岁,你在星期五出生)但我在代码中遇到问题(参数’fmt’未填充:11 )
代码:
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'))```
输出:
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```
-
Corralien 评论
简单胜于复杂。 (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]}')
输出:
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
2年前