简体   繁体   English

错误:必须为整数(元组类型为元组)

[英]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.

相关问题 获取“需要整数(得到类型元组)”错误,使用 cv2 绘制矩形 - Getting an "integer is required (got type tuple)" error, drawing a rectangle using cv2 TypeError:需要一个 integer(获取类型元组)<python><opencv><tesseract></tesseract></opencv></python> - TypeError: an integer is required (got type tuple) <python> <OpenCV> <tesseract> Python TypeError:需要一个整数(得到类型元组)-(OpenCV / Numpy) - Python TypeError: an integer is required (got type tuple) - (OpenCV / Numpy) 枕头图像TypeError:需要一个整数(元组类型元组) - pillow image TypeError: an integer is required (got type tuple) 类型错误:需要一个 integer(获取类型元组)日期时间 Python - TypeError: an integer is required (got type tuple) datetime Python 类型错误:需要一个 integer(获取类型元组)|| python 中的网站阻止代码 - TypeError: an integer is required (got type tuple) || website blocking code in python 类型错误:需要一个整数(得到类型 str)-错误 - TypeError: an integer is required (got type str)-error 类型错误:需要一个整数(获取类型 datetime.datetime) - Type error : an integer is required (got type datetime. datetime) 整数是必需的(得到类型 str) - integer is required(got type str) Python多处理日志记录错误-TypeError:必须为整数(获取类型为NoneType) - Python Multiprocessing Logging Error - TypeError: an integer is required (got type NoneType)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM