0
"""File for creating Person objects"""

class Person:
    """Defines a Person class, suitable for use in a hospital context.
    Data attributes: name of type str
                     age of type int
                     weight (kg) of type float
                     height (metres) of type float
    Methods: bmi()
             status()
    """
    def __init__(self, name, int_age, weight, height):
        """Creates a new Person object with a specified name, age, weight, and
        height."""
        self.name = name
        age = int(int_age)
        self.age = age
        self.weight = weight
        self.height = height

    def bmi(self):
        """Returns the body mass index of the person"""
        return self.weight / (self.height * self.height)

    def status(self):
        """ghnwjghwjgwe"""
        if self.bmi() < 18.5:
            return "Underweight"
        if self.bmi() >= 18.5 and self.bmi() < 25:
            return "Normal"
        if self.bmi() >= 25 and self.bmi() < 30:
            return "Overweight"
        if self.bmi() >= 30:
            return "Obese"
    def __str__(self):
        """Returns the formatted string represent of the Person object"""
        name = self.name
        age = self.age
        bmi = self.bmi()
        status = self.status()
        template = "{0} ({1}) has a bmi of {2:3.2f}. Their status is {3}."
        return template.format(name, age, bmi, status)    
def read_people(csv_filename):
    """gkhwuigjwh"""
    persons = []  # list for Person objects
    with open(csv_filename, "r") as file:  
        for line in file:
            args = line.split(",")
            for i in range(1, len(args)):  # convert arguments to float
                args[i] = float(args[i])
            persons.append(Person(*args))  # add Person objects to list
    return persons  
def filter_people(people, status_string):
    """gfjhwjgwhgw"""
    persons = []
    for peoples in people:
        if peoples.status() == status_string:
            persons.append(peoples)
    return persons

I have a data file of:

Peter Normal,23,89.4,1.82
Polly Perkins,47,148.8,1.67
Griselda Gribble,92,48,1.45
Ivan Ng,19,59,2.0
Lucy Lovelorn,14,50,1.6
Leslie McWhatsit,70,59.2,1.65

Using the functions age_value(person) and bmi_value(person), I want to have the code looking like:

Lucy Lovelorn (14) has a bmi of 19.53. Their status is Normal.
Ivan Ng (19) has a bmi of 14.75. Their status is Underweight.
Peter Normal (23) has a bmi of 26.99. Their status is Overweight.
Polly Perkins (47) has a bmi of 53.35. Their status is Obese.
Leslie McWhatsit (70) has a bmi of 21.74. Their status is Normal.
Griselda Gribble (92) has a bmi of 22.83. Their status is Normal.

This is ordered by age. How do I get started on this, will it look something like this?

def age_value(person):
    """Returns a Person's name"""
    people.sort(key=age_value)
    return person.name
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You're using `key=age_value` inside the `age_value()` function. That's infinite recursion. – Barmar May 18 '22 at 02:40
  • Take the `people.sort()` call out of the function, call it after the function is defined. – Barmar May 18 '22 at 02:41
  • Why are you returning `person.name` instead of `person.age`? – Barmar May 18 '22 at 02:41
  • There's really no need for a separate function, just use `key = lambda p: p.age` – Barmar May 18 '22 at 02:42
  • Welcome to Stack Overflow. I can't understand the question. You are saying that you wrote the code to read the file, and create a list with `Person` instances? And now you want to sort them... according to what rule? And what else are you actually having difficulty with? Aside from the sorting, can you get output that looks right? – Karl Knechtel May 18 '22 at 02:45

0 Answers0