Вы смешиваете атрибуты класса и атрибуты экземпляра. Атрибуты класса общие между экземплярами:
class Banks:
total = 0
def __init__(self,name,money):
self.name=name
self.money=money
Banks.total += money
b1 = Banks("one",100)
b2 = Banks("two",5000000)
# prints 5000100 - all the money of all banks,
# does not help you comparing avgs ov instances at all
print(b1.total)
Выход:
5000100
Вам нужны отдельные средние значения для каждого экземпляра и функция, которая сравнивает один экземпляр (self
) и агенты other
:
class School:
def __init__(self):
# self.n =int(input()) # not needed - use len(one of your lists)
list_ages = [float(x) for x in input("Gimme ages, space seperated: ").split()]
list_hight = [float(x) for x in input("Gimme hights, space seperated: ").split()]
list_weight = [float(x) for x in input("Gimme weights, space seperated: ").split()]
# shortest list downsizes all other lists
self.list_ages, self.list_hight, self.list_weight = zip(
*zip( list_ages,list_hight,list_weight ))
def get_av(self):
self.avg_age = sum(self.list_ages) / len(self.list_ages)
print(self.avg_age)
self.avg_height = sum(self.list_hight) / len(self.list_hight)
print(self.avg_height)
self.avg_weight = sum(self.list_weight) / len(self.list_weight)
print(self.avg_weight)
return self.avg_age, self.avg_height, self.avg_weight
def compare(self,other):
self.get_av()
other.get_av()
print("Our pupils are younger: ", self.avg_age < other.avg_age)
print("Our pupils are smaller: ", self.avg_height < other.avg_height)
print("Our pupils are lighter: ", self.avg_weight < other.avg_weight)
c = School() # 4 5 6 22 44 66 88 99 20.2 20.2 20.2 20.2 20.2 20.2
d = School() # 100 100 100
c.compare(d)
Вывод (отформатированный с новой строкой между ними):
Gimme ages, space seperated: 4 5 6
Gimme hights, space seperated: 22 44 66 88 99
Gimme weights, space seperated: 20.2 20.2 20.2 20.2 20.2 20.2
Gimme ages, space seperated: 100
Gimme hights, space seperated: 100
Gimme weights, space seperated: 100
5.0
44.0
20.2
100.0
100.0
100.0
Our pupils are younger: True
Our pupils are smaller: True
Our pupils are lighter: True
Дополнительная информация: