Problem with python objects


#1

I am given the following data-file:

stars.csv

Peter Piper,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

I have been given a class definition (and some methods) and was asked to add some functions. This I did as shown:

class Person:
    """Defines a Person class """
    def __init__(self, name, age, weight, height):
        """ Create a new Person object"""
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

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

    def status(self):
        """  status """
        bmi = self.bmi()
        if bmi < 18.5:
            status = "Underweight"
        elif 18.5 <= bmi < 25:
            status = "Normal"
        elif 25 <= bmi < 30:
            status = "Overweight"
        elif bmi >= 30:
            status = "Obese"
        return status

    def __str__(self):
        """ Output data """
        answer = "{0} ({1}) has a bmi of {2:.02f}. Their status is {3}."
        return answer.format(self.name, self.age, self.bmi(), self.status())

def read_stars(csv_filename):
    """reads file and sorts it then runs through a class"""
    items = []
    file = open(csv_filename, 'r')
    for row in file:
        name, age, weight, height = row.split(',')
        items.append(Person(name, int(age), float(weight), float(height)))
    file.close()
    return items

Then I need to a create a new function filter_people(people, status) that takes a list of people and a “status” string and returns a new list containing just the people from the original list whose health status equals the status parameter. So far, so good. So, where am I stuck? In manipulating the filter_stars GLOBAL function. Is it possible to create a global function filter_stars(people, status) such that the output will be classified based on their bmi and status?

Test code

bods = read_stars("stars.csv")
for status in ['Underweight', 'Normal', 'Overweight', 'Obese']:
    bods_with_status = filter_stars(bods, status)
    print("People who are {}:".format(status))
    for bod in bods_with_status:
        print(bod)
    print()

Expected Output

People who are Underweight:
Ivan Ng (19) has a bmi of 14.75. Their status is Underweight.

People who are Normal:
Griselda Gribble (92) has a bmi of 22.83. Their status is Normal.
Lucy Lovelorn (14) has a bmi of 19.53. Their status is Normal.
Leslie McWhatsit (70) has a bmi of 21.74. Their status is Normal.

People who are Overweight:
Peter Piper (23) has a bmi of 26.99. Their status is Overweight.

People who are Obese:
Polly Perkins (47) has a bmi of 53.35. Their status is Obese.

I am totally stuck as to come up with a function that can output as above. Please help with writing the global function filter_stars(people, status)

Help appreciated.


#2

A post was merged into an existing topic: How to handle a set of objects in Python


#3