[英]Error : an integer is required (got type tuple)
I am stuck to return age from birth date. 我被迫从出生日期返回年龄。 What I want to do is to return exact age from inputed birth date with using class.
我想做的是使用类从输入的出生日期返回确切的年龄。 My current code is :
我当前的代码是:
from datetime import datetime, date
class Person(object):
def __init__(self, first_name, last_name, birth_date):
self.first_name = first_name
self.last_name = last_name
self.birth_date = birth_date
def full_name(self):
return '{} {}'.format(self.first_name, self.last_name)
def age(self):
return date.today() - date(self.birth_date)
std = Person("aaa", 'bbb', (1990, 10, 11))
print(std.age())
I have to also return full name from inputed first name and last name respectively but that was already done. 我还必须分别从输入的名字和姓氏返回全名,但这已经完成了。 It would be really appreciated if it is explained in detail.
如果对其进行详细说明,将不胜感激。
You can simply use: 您可以简单地使用:
def age(self):
return date.today() - date(year=self.birth_date[0], month=self.birth_date[1],
day=self.birth_date[2])
and it should return you: 9890, 00:00:00
它应该返回您:
9890, 00:00:00
: 9890, 00:00:00
: 9890, 00:00:00
If you want to compute age in years, the easiest way would be: 如果要以年为单位计算年龄,最简单的方法是:
def age(self):
from dateutil.relativedelta import relativedelta
return relativedelta(date.today(), date(year=self.birth_date[0],
month=self.birth_date[1],
day=self.birth_date[2])).years
and the above should return you: 27
以上将返回您:
27
You can also do: 您也可以这样做:
def age(self):
from dateutil.relativedelta import relativedelta
age = relativedelta(date.today(), date(year=self.birth_date[0],
month=self.birth_date[1],
day=self.birth_date[2]))
return str(age.years) + ' years, ' + str(age.days) + ' days'
and the above should return you: 27 years, 28 days
以上内容将返回您:
27 years, 28 days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.