I have created 12 people with age, gender, height, and weight with OOP class, however, I don't understand how I can make it that it picks specific stuff that I want from the available classes. for example
class Person:
def __init__(self, age, gender, height, weight):
self.height = height
self.weight = weight
self.age = age
self.gender = gender
person1 = Person(19, "Female", 162, 63)
print(person1.weight)
person2 = Person(23, "Male", 170, 89)
print(person2.weight)
person3 = Person(34, "Male", 178, 88)
print(person3.weight)
person4 = Person(44, "Female", 169, 66)
print(person4.weight)
person5 = Person(23, "Female", 166, 64)
print(person5.weight)
person6 = Person(25, "Male", 180, 78)
print(person6.weight)
person7 = Person(18, "Female", 174, 70)
print(person7.weight)
person8 = Person(23, "Male", 190, 90)
print(person8.weight)
person9 = Person(34, "Female", 177, 72)
print(person9.weight)
person10 = Person(27, "Male", 187, 85)
print(person10.weight)
person11 = Person(22, "Male", 184, 80)
print(person11.weight)
person12 = Person(41, "Female", 158, 69)
print(person12.weight)
I want to find the average weight of males in this category, I just don't understand how I can make the code to choose specifically from males and only to average their weight, I added the height and age just for like creating a real environment but these numbers are not important since I only want to find the males weight that's all.
CodePudding user response:
One way you can do this is like this. First, we can find all of the people using the module gc
. Looping through gc.get_objects()
, we can check if each instance in the program is an instance of Person
. Then, we can increment a variable for the number of male people and increment a variable for the total weight. Then we can divide. This is one way of writing code for this:
import gc
class Person:
def __init__(self, age, gender, height, weight):
self.height = height
self.weight = weight
self.age = age
self.gender = gender
total_weight = 0
number_of_males = 0
for instance in gc.get_objects():
if isinstance(instance, Person):
if instance.gender == "male":
number_of_males = 1
total_weight = instance.weight
print(total_weight / number_of_males)
Or, using list comprehension we can do this:
import gc
male_weights = [instance.weight for instance in gc.get_objects() if (isinstance(instance, Person) and instance.gender == "male")]
print(sum(male_weights) / len(male_weights))
However, you can test them since I don't have time to test them.
CodePudding user response:
You have created 12 'Person' objects, from person1 till person12. In each print statement, you printed the weight field of each Person you created (from person1 till person12).
For average, the formula in this case is
(sum of weights where gender is "Male") / (amount of Person objectswhere gender is "Male")
Then first you should go over all your objects. There is a naive way of doing this:
male_weight_sum = person2.weight person3.weight person6.weight person8.weight person10.weight person11.weight
total_males = 6 # after counting
average_male_weight = male_weight_sum / total_males
print(average_male_weight)
I guess that this code looks too long, especially in the first line. And did you notice I needed to check each person, and find out its gender? That's not realistic in programming.
A better solution which is less obnoxious, is to prevent using many variables of the same time and same destination. You can use a list instead. Defining a list is made by this way:
person_list = [] # List without items
Now lets add the 12 Person objects you created:
person_list = [Person(19, "Female", 162, 63), Person(23, "Male", 170, 89), Person(44, "Female", 169, 66), Person(23, "Female", 166, 64), Person(25, "Male", 180, 78), (Person(18, "Female", 174, 70), Person(23, "Male", 190, 90), Person(34, "Female", 177, 72), Person(27, "Male", 187, 85), Person(22, "Male", 184, 80), Person(41, "Female", 158, 69)]
Or append all Person objects inline:
person_list = [
Person(19, "Female", 162, 63),
Person(23, "Male", 170, 89),
Person(44, "Female", 169, 66),
Person(23, "Female", 166, 64),
Person(25, "Male", 180, 78),
Person(18, "Female", 174, 70),
Person(23, "Male", 190, 90),
Person(34, "Female", 177, 72),
Person(27, "Male", 187, 85),
Person(22, "Male", 184, 80),
Person(41, "Female", 158, 69)]
Now, after we have a list with the items we need to analyze, we need to sum males' weights, to calculate how many males are in this list, and then calculate the average:
male_weight_sum = 0
total_males = 0
average_male_weight = 0
for i in range(len(person_list)): # Python looping through range, from 0 to 11
if person_list[i].gender == "Male": # We only care about males
male_weight_sum = male_weight_sum person_list[i].weight
total_males = total_males 1
average_male_weight = male_weight_sum / total_males) # You should also pre-check if total_males > 0, because you can't divide by zero
print(average_male_weight)