Determining the Percentage of Voter Population - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Determining the Percentage of Voter Population (/thread-23400.html) |
Determining the Percentage of Voter Population - Than999 - Dec-28-2019 Hi, I am struggling with this assignment. I am asked to determine which county has the highest voter percentage (in decimal form only, i.e. population in the county/voters in the county) in each of the given county below. The assignment calls for the output and print statement of the tuple of the 'name of the county' and the 'voter percentage'. I've already created my class 'County' and the function for calculating the percentage of voters in each of the county, and then compare the initial voter percentage with the first county withe rest of the county in the list ('data'). Once the voter percentage is compared and determined with the other county voter percentage as to which has the highest voter percentage, it is updated as that voter percentage with that population. What is wrong with my function? How can I print a tuple with only the name of the county with its highest voter percentage? class County: def __init__(self, name, population, voters): self.name = name self.population = population self.voters = voters def highest_turnout(data): people_county = data[0] total_people_county = data[0].population people_voted = data[0] total_number_voted = data[0].voters for County in data: voter_percentage = total_number_voted/total_people_population if voter_percentage > (data.voters/data.population): people_county = data total_people_county = data.population people_voted = data total_number_voted = data.voters return voter_percentage and total_people_voted # your program will be evaluated using these objects # it is okay to change/remove these lines but your program # will be evaluated using these as inputs 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] result = highest_turnout(data) # do not change this line! print(result) # prints the output of the function # do not remove this line! the format for each county in the given list is ('name of county', 'population', 'voters') RE: Determining the Percentage of Voter Population - ibreeden - Dec-28-2019 I believe you misinterpret the assignment. The "highest_turnout()" function should not be part of the "County" class because the County class gives the definition how to create one county object. "highest_turnout()" should iterate over all counties so it is not logical to situate this function in the class. In the first part the county-objects are created with: allegheny = County("allegheny", 1000490, 645469) et ceteraThis uses only the __init__ function of the class. After that a list is created of all county objects: data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]Then your function "highest_turnout()" is called: result = highest_turnout(data) # do not change this line! print(result) # prints the output of the functionSo your assignment is to create a function with the following specifications:
RE: Determining the Percentage of Voter Population - Than999 - Dec-28-2019 Hi I am still not clear what you mean by "the County class gives the definition how to create one county object"? Can you elaborate? Because the assignment starts off with this code: # implement County class here def highest_turnout(data) : # implement the function here return # modify this as needed # your program will be evaluated using these objects # it is okay to change/remove these lines but your program # will be evaluated using these as inputs 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] result = highest_turnout(data) # do not change this line! print(result) # prints the output of the function # do not remove this line! RE: Determining the Percentage of Voter Population - snippsat - Dec-28-2019 Some hints. If i run the code,i can loop over data like this. >>> for country in data: ... print(country.name, country.population, country.voters) ... allegheny 1000490 645469 philadelphia 1134081 539069 montgomery 568952 399591 lancaster 345367 230278 delaware 414031 284538 chester 319919 230823If eg make this function for percentage. def percentage(voters, population): return 100 * voters / population >>> for country in data: ... print(country.name, percentage(country.voters, country.population)) ... allegheny 64.51528750912053 philadelphia 47.53355359978696 montgomery 70.23281401594511 lancaster 66.67631823538439 delaware 68.72383951926305 chester 72.15045058280377 bucks 72.0064662984719So this is stuff that you can have in highest_turnout function.Now can try find highest voter percentage in the loop,or collect data and use max() if that allowed.Place this function highest_turnout outside of class,not in the class as you have in first code.Can place a function in class,as just a function with using @staticmethod .
RE: Determining the Percentage of Voter Population - ichabod801 - Dec-28-2019 The class should just do this: store the information given for one county, and calculate that county's voter percentage. Then you create an instance of the class for each county, which will automatically get the voter percentages. Then you can use that information to find the largest voting percentage. If you've studied overriding special methods like __lt__ and __eq__, you can use those so that class instances sort themselves by voting percentage. Otherwise, you could do a sort with a key parameter to sort by the instances' voter percentage attribute. RE: Determining the Percentage of Voter Population - DeaD_EyE - Dec-29-2019 You can use the max function with lambda:max(data, key=lambda vote: vote.voters / vote.population).nameInstead you could write it as a function, which is easier to unterstand. def relative(country): return country.voters / country.population max(data, key=relative).nameJust adding the calculated percentages to the class is a better solution. from functools import total_ordering @total_ordering class County: def __init__(self, name, population, voters): self.name = name self.population = population self.voters = voters self.relative = voters / population def __lt__(self, other): return self.relative < other.relative def __gt__(self, other): return self.relative > other.relative def __eg__(self, other): return self.relative == other.relativeNow you can apply max on the data list. The function max returns the whole object from the list. The total_ordering decorator modifies the class. It will create the missing comparisons like <= >= and != .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] result = max(data) print(result, type(result), result.name) As you can see, the representation is missing. It could be added with the method __repr__, but it's not a must.Dataclasses could do this automatically. Since Python 3.7 they are available. from dataclasses import dataclass, field @dataclass(order=True) class County: name: str = field(compare=False) population: int = field(compare=False) voters: int = field(compare=False) relative: float = field(compare=True, default=0.0) def __post_init__(self): self.relative: float = self.voters / self.population County('Test', 100, 1) So it has also a nice presentation. In this case the relative value is set to 0.0.After __post_init__ was called by dataclass itself,the self.relative is overwritten with the calculated value. You can also define how dataclasses are sorted. For example you can make more then on field to compare, but you have to exclude other fields, if they are not used for sorting. In this case relative is the only field to compare. |