Суммируя предложения из комментариев, функция highest_turnout
должна return
highest
, в противном случае после завершения функции значение highest
будет потеряно.Затем вместо передачи от self
до highest_turnout
введите data
:
class County:
def __init__(self, init_name, init_population, init_voters):
self.name = init_name
self.population = init_population
self.voters = init_voters
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]
def highest_voter_turnout(data):
'''iterate over county objects comparing county.voters values;
returns county object with max voters attribute'''
highest_voters = data[0]
for county in data:
if county.voters > highest_voters.voters:
highest_voters = county
return highest_voters
result_highest_voter_turnout = highest_voter_turnout(data)
print(result_highest_voter_turnout.name)
До сих пор это будет возвращать и отображать название округа, который: (i) имеет самый высокий голосующийявка "(т. е. allegheny
).
Теперь можно создать аналогичную функцию для вычисления округа с наибольшим" (ii) процентом проголосовавших "(один метод также упоминается в комментариях).