-1

This is my code so far. In fact, I completed the requirements but the test made it harder when checking the result by requiring my code to be more flexible with the 'person' I made. I've been stuck on this for about 2 days so I really hope someone can help me make my code more flexible by being able to change people and their birthdate just as the extra requirements.

 from datetime import date

class person:
    pass

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

def get_age(person):
    birthdate = date(1976, 8, 14)
    today = date.today()
    age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
    return 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 I got:

Write a class definition for the Person class and write user-defined functions with these function headers:

def create_person(name, height, birthdate):
  # Return a a 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, 2022. if Mary was born on June 4, 2020, then

Mary's age is 2. However, if Bob was born on June 14, 2020, then Bob would not have had a second birthday yet so the age is 1.

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 For example, if Michael's height is 190cm and his age is 45, the string object

returned from this function should be: Michael is 190 cm high and is 46 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.
      # are integers Here is a sample run of a main program that just calls the main function.

Michael is 190 cm high and is 46 years old.

And this is a hint I 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

And this is the result when I test my following code:

#TEST 1#
main() returned None
inputs:

outputs:
Michael is 190 cm high and is 46 years old.
----------
#TEST 2#
** ERROR **get_description(sara) returned Georgia is 170 cm high and is 46 years old.
* EXPECTED * Sara is 160 cm high and is 20 years old.
inputs:

outputs:
----------
#TEST 3#
** ERROR **get_age(sara) returned 46
* EXPECTED * 20
inputs:

outputs:
----------
#TEST 4#
** ERROR **get_age(eric) returned 46
* EXPECTED * 10
inputs:

outputs:
----------
#TEST 5#
** ERROR **get_age(carter) returned 46
* EXPECTED * 11
inputs:

outputs:
----------
#TEST 6#
** ERROR **get_age(georgia) returned 46
* EXPECTED * 14
inputs:

outputs:
----------
4
  • 1
    What's the point of the class if all the "methods" are defined outside of it? Commented Oct 17, 2022 at 17:03
  • The class itself serves the purpose of your create_person function; you should probably read the section of the tutorial on classes. Whoever wrote this assignment is doing you a disservice. (Or one can hope that this is just a lead-in to how the class should really be defined.) Commented Oct 17, 2022 at 17:04
  • 2
    You didn't use class correctly here. It's a pretty good attempt but the syntax and use of classes is very off, because the class you built is empty. The problem is that you aren't creating instances of people, you're just creating and modifying class attributes rather than instance attributes Your method was bizarre but understandable, but the problem is that with this only one person can be stored at a time because no instances are created, and creating a new person will delete all information about the person you previously tried to create. Commented Oct 17, 2022 at 17:08
  • 2
    That's why only one set of values has been saved and in the test run only info about Georgia is being output. Commented Oct 17, 2022 at 17:11

2 Answers 2

2

create_person() needs to create an instance of the Person class, not assign attributes of the class itself. Otherwise you can only have one person -- you're using the class as a global variable.

class Person:
    pass

def create_person(name: str, height: int, birthdate: datetime.date) -> Person:
    person = Person()
    person.name = name
    person.height = height
    person.age = birthdate
    return person

Note that this is an unusual way to define the class. Normally you would initialize the attributes in the class's __init__() method.

class Person:
    def __init__(self, name: str, height: int, birthdate: datetime.date):
        self.name = name
        self.height = height
        self.age = birthdate

def create_person(name: str, height: int, birthdate: datetime.date) -> Person:
    return Person(name, height, age)
Sign up to request clarification or add additional context in comments.

Comments

0

Don't forget to create the Person() object inside the create_person function. This code worked well:

    from datetime import date
    
    class Person:
        pass
    
    def create_person(name, height, birthdate):
        person = Person()
        person.name = name
        person.height = height
        person.age = birthdate
        return person
    
    def get_age(person):
        birthdate = person.age
        today = date.today()
        age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
        return 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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.