Python Forum
Python Objects and functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Objects and functions
#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) as shown:

class Person:
   [font=Courier New, Courier, monospace] """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[/font]
I need to a create a new function filter_stars(stars, 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.

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 finding it impossible to come up with a global function that can output as above.

Help appreciated.
Reply
#2
(Oct-08-2018, 05:42 AM)jill1978 Wrote: I am finding it impossible to come up with a global function that can output as above.
You don't have to come up with a function that produce that output. Your function need to take two arguments - stars and status and then return a list/tuple of people from stars with status status.
As you can see your function will be called several times with different status argument.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and objects beginning python 3 pangea 2 3,144 Dec-15-2017, 12:35 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020