简体   繁体   English

Python 编程与 Class 定义

[英]Python Programming with Class Definition

This is my code so far, the error I'm having so far is (name 'person' is not defined) on line person.name = name , I'm literally stuck & trying to figure out what is the issue/error since my code has to be based on the question I wrote below.I'm not sure if the remaining of my code has any errors since it is detecting that error first.到目前为止,这是我的代码,到目前为止我遇到的错误是 (name 'person' is not defined) on line 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))

This is the question:这是问题:

Write a class definition for the Person class and write user-defined functions with these function headers:为 Person class 编写 class 定义,并使用这些 function 头文件编写用户定义的函数:

 def create_person(name, height, birthdate): # Return aa new person object with the given name, height and birthdate. # - name is a str # - height is an int object in centimetres # - birthdate is a date object from the # module datetime

 def get_age(person): # Return the age of the person in years.

For example, assume today's date is June 12, 2018. if Mary was born on June 4, 2017, then Mary's age is 1. However, if Bob was born on June 14, 2018, then Bob would not have had a first birthday yet so the age is 0.例如,假设今天的日期是 2018 年 6 月 12 日。如果 Mary 出生于 2017 年 6 月 4 日,那么 Mary 的年龄是 1。但是,如果 Bob 出生于 2018 年 6 月 14 日,那么 Bob 还没有过第一个生日所以年龄为0。

 def get_description(person): # Return a string object of the form: Name is # N cm high and is M years old, where N and M # are integers

For example, Michael is 190 cm high and is 43 years old or Samantha is 95 cm high and is 4 years old.例如,迈克尔身高 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.

If you use a function from an imported module when writing your function, you usually declare the import statement at the top of your code.如果在编写 function 时使用来自导入模块的 function,则通常在代码顶部声明导入语句。

Here is a sample run of a main program that just calls the main function.这是一个只调用主 function 的主程序的示例运行。

 Michael is 190 cm high and is 43 years old.

This is a hint I currently received:这是我目前收到的提示:

Use the date class from the datetime module to represent a date.使用 datetime 模块中的日期 class 来表示日期。 An object whose type is date, has attributes: year, month and day that you can use to compute the age of a Person.类型为日期的 object 具有可用于计算人员年龄的年、月和日属性。

To compute the current age of a person, you will need to first compute today's date.要计算一个人的当前年龄,您需要首先计算今天的日期。 There is a method in the date class of the datetime module that creates a new date object that represents the current date.在 datetime 模块的日期 class 中有一个方法可以创建一个新的日期 object 来表示当前日期。 The name of this method is today.这种方法的名称是今天。 However, the special argument of this method must be the date class itself, instead of a particular object whose type is date.但是,此方法的特殊参数必须是日期 class 本身,而不是类型为日期的特定 object。 A method that is applied to a class object instead of to an instance of that class is called a class method.应用于 class object 而不是该 class 的实例的方法称为 ZA2F2ED4F8EBC2CBB4C21A29DC4 方法。

Therefore, to create the current date you can use the expression:因此,要创建当前日期,您可以使用以下表达式:

 date.today()

since after importing the date class from the datetime module, the identifier date is bound to the date class object.因为从 datetime 模块导入日期 class 后,标识符 date 绑定到日期 class object。

To compute the age you can just subtract the year attribute of the birthdate from the year attribute of the current date.要计算年龄,您只需从当前日期的年份属性中减去出生日期的年份属性即可。 However, you will also need to check whether the person has already had their birthday yet this year and if not, subtract one year但是,您还需要检查此人今年是否已经过生日,如果没有,则减去一年

Have Person create its own attributesPerson创建自己的属性

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)

This produces这产生

myname

Now the other functions will have a class instance they can use.现在其他函数将拥有一个可以使用的 class 实例。

If you have a problem with any one of the functions, that's best posted in another question that focuses just on that problem.如果您对其中任何一个功能有疑问,最好将其发布在另一个仅关注该问题的问题中。 (removing functions / methods not needed to demonstrate the problem). (删除不需要演示问题的功能/方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM