Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用类定义进行Python编程_Python_Python 3.x_Python 2.7 - Fatal编程技术网

用类定义进行Python编程

用类定义进行Python编程,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,这是到目前为止我的代码,到目前为止我遇到的错误是没有在person行定义name'person'。name=name,我真的被卡住了&试图找出问题/错误,因为我的代码必须基于我在下面写的问题。我不确定我的代码的剩余部分是否有任何错误,因为它首先检测到错误 from datetime import date class person: pass def create_person(name, height, birthdate): person.name = name

这是到目前为止我的代码,到目前为止我遇到的错误是没有在person行定义name'person'。name=name,我真的被卡住了&试图找出问题/错误,因为我的代码必须基于我在下面写的问题。我不确定我的代码的剩余部分是否有任何错误,因为它首先检测到错误

from datetime import date

class person:
    pass

def create_person(name, height, birthdate):
    person.name = name
    person.height = height
    get_age = birthdate
    return person

def get_age(person):
    birthdate = person.birth
    today = date.today()
    subyear = 0
    if today.month < birthdate.month or (today.month == birthdate.day and today.day <= birthdate.day):
        subyear = 1
    person.age = (today.year - (birthdate.year + subyear))
    return person.age

def get_description(person):
    return person.name + ' is ' + str(person.height) + ' cm high and is ' + str(get_age(person)) + ' years old'

def main():
    birthdate = date(1976, 8, 14)
    person = create_person('Michael', 190, birthdate)
    print(get_description(person))
例如,假设今天的日期是2018年6月12日。如果玛丽出生在 2017年6月4日,玛丽的年龄是1岁。然而,如果鲍勃出生在六月 2018年11月14日,那么Bob就不会有一岁的生日了 是0

例如,迈克尔身高190厘米,43岁,萨曼莎是 95厘米高,4岁

def main():
# Create a person named 'Michael', with height
# 190 cm, who was born on August 14, 1976 and
# output a description of this individual.
如果在编写代码时使用导入模块中的函数 函数,通常在 代码

下面是一个只调用main函数的主程序的运行示例 功能

Michael is 190 cm high and is 43 years old.
这是我目前收到的提示:

使用datetime模块中的date类表示日期。一 类型为date的对象具有以下属性:年、月和日期 你可以用它来计算一个人的年龄

要计算一个人的当前年龄,首先需要计算 今天的日期。datetime的date类中有一个方法 创建表示当前日期的新日期对象的模块 日期这个方法的名字是今天。然而,特别的论点 此方法的名称必须是date类本身,而不是特定的 类型为日期的对象。应用于类对象的方法 该类的实例称为类方法,而不是类方法

因此,要创建当前日期,可以使用以下表达式:

date.today()
因为从datetime模块导入date类之后 标识符日期绑定到日期类对象

要计算年龄,只需减去 当前日期的年份属性中的生日。然而,你 还需要检查该人员是否已经接受了治疗 今年还没有过生日,如果没有,减去一年

让人创建自己的属性

class Person:

    def __init__(self, name, height, birthdate):
        self.name = name
        self.height = height
        self.birthdate = birthdate
        
# why someone would write such a function is beyond me, but...
def create_person(name, height, birthdate):
    return Person(name, height, birthdate)

# test
p = create_person('myname', 100, '23-23-23')
print(p.name)
这就产生了

myname
现在,其他函数将有一个可以使用的类实例


如果您对其中任何一个函数有问题,最好在另一个问题中发布,只关注该问题。删除不需要演示问题的函数/方法。

您应该缩进这些方法,并将self作为参数包含到方法中。person.name=name应该是self.name=name这实在太多了。您应该尝试创建一个,重点放在minimal.person上,这一行实际上没有定义person。你为什么认为会这样?我认为这个练习不是很好,编写一个实例化类的函数而不是直接使用uu init_u_u,这有点奇怪,但你应该阅读。@stefan-这很有道理,但问题需要其他方法。这一定是一门关于如何编写非常糟糕的python的课程。@jornsharpe我自己认为这很奇怪,但主题是介绍我如何创建一个类。在def create_personname,height,birthdate:的定义下,它表示返回一个具有给定名称,height和birthdate的新person对象。name是一个str,height是一个int对象,单位为厘米,birthdate是模块datetime中的一个date对象,我认为它是系统输入的任何东西,因此如果它使用例如:name=John,height=200cm,brithdate=1976年5月15日。我尝试了你提到的代码&它不起作用。它说“person”这个名字没有定义。我用一个例子更新了它,说明类实例是正确创建的。若你们得到的对象并没有参数,那个么你们在做别的事情。老实说,我不知道如何在不做object'something'的情况下获得错误消息,所以我无法很好地猜测您为什么会得到该消息。
date.today()
class Person:

    def __init__(self, name, height, birthdate):
        self.name = name
        self.height = height
        self.birthdate = birthdate
        
# why someone would write such a function is beyond me, but...
def create_person(name, height, birthdate):
    return Person(name, height, birthdate)

# test
p = create_person('myname', 100, '23-23-23')
print(p.name)
myname