Advertisement
Cobble5tone

Educational - Iterating through list of classes

Aug 30th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. # implement County class here
  2. class County:
  3.     def __init__(self, init_name, init_population, init_voters):
  4.         self.name = init_name
  5.         self.population = init_population
  6.         self.voters = init_voters
  7.         self.turnout = self.voters / self.population
  8.  
  9.  
  10. def highest_turnout(data):
  11.     # implement the function here
  12.  
  13.     maxCounty = ''  # You want the variable to match the data type you're comparing, so string
  14.     maxTO= 0  # Holds the greatest maxTO
  15.     for item in data:  # Iterates through items in data using item as iterator
  16.         if item.turnout > maxTO:
  17.             maxCounty = item.name  # Sets maxCounty to name of county if condition is met
  18.             maxTO = item.turnout  # Sets maxTo to turnout if higher than current maxTO
  19.  
  20.     print(maxCounty)
  21.     print(maxTO)
  22.  
  23.     tuple = (maxCounty, maxTO)
  24.     print('this is the tuple', tuple)
  25.     return tuple  # modify this as needed
  26.  
  27.  
  28. # your program will be evaluated using these objects
  29. # it is okay to change/remove these lines but your program
  30. # will be evaluated using these as inputs
  31. allegheny = County("allegheny", 10000, 6500)
  32. philadelphia = County("philadelphia", 100, 25)
  33. montgomery = County("montgomery", 500, 475)
  34. lancaster = County("lancaster", 7000, 798)
  35. data = [allegheny, philadelphia, montgomery, lancaster]
  36.  
  37. result = highest_turnout(data)  # do not change this line!
  38. print(result)  # prints the output of the function
  39. # do not remove this line!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement