简体   繁体   中英

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.

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:

 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.

 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.

 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.

Here is a sample run of a main program that just calls the main 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. An object whose type is date, has attributes: year, month and day that you can use to compute the age of a Person.

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. 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. A method that is applied to a class object instead of to an instance of that class is called a class method.

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.

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 attributes

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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