-3

I am not sure why I am getting this error.

I am getting this error: Error on line 24: tempmax = ans ^ IndentationError: expected an indented block

Here what I´ve have so far:

 def __init__(self, init_name, init_population, init_voters):
  self.name = init_name
  self.population = init_population
  self.voters = init_voters

def get_init_population(self):
 return self.population

def get_init_voters(self):
 return self.voters

def get_name(self):
 return self.name

def highest_turnout(data):
 tempcounty = ""
 tempmax = 0

for county in data:
 ans = (county.get_init_voters()/county.get_init_population())
 if ans > tempmax:

tempmax = ans
tempcounty = county
 return [(tempcounty.get_name(), tempmax)]

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!```
  • your formatting is kinda off here, python is generally indented with 4 spaces and it looks like your for loop, if, and return are not properly indented – bherbruck May 29 '20 at 13:10
  • 2
    If you don't know why that line causes an indentation error, then you have skipped learning the basics of Python. – khelwood May 29 '20 at 13:11
  • If you use four spaces for indentation, it makes it easier to *see** the indentation level. Please fix the formatting. .. [Formatting help](https://stackoverflow.com/editing-help#code) ... [more Formatting](https://stackoverflow.com/editing-help) ... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii May 29 '20 at 13:11
  • Because, as the error message states, the block starting with `tempmax = ...' should be indented. Also, you should respect the convention of using 4 spaces for indents in Python, that would make the structure of your code and the different levels easier to distinguish. – Thierry Lathuille May 29 '20 at 13:12

1 Answers1

0

The indentation is not correct, try:

class County:
    def __init__(self, init_name, init_population, init_voters):
        self.name = init_name
        self.population = init_population
        self.voters = init_voters

    def get_init_population(self):
        return self.population

    def get_init_voters(self):
        return self.voters

    def get_name(self):
        return self.name


def highest_turnout(data):
    tempcounty = ""
    tempmax = 0
    for county in data:
        ans = (county.get_init_voters()/county.get_init_population())
        if ans > tempmax:
            tempmax = ans
            tempcounty = county
    return [(tempcounty.get_name(), tempmax)]

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

Output:

[('chester', 0.7215045058280377)]
bherbruck
  • 2,167
  • 1
  • 6
  • 17
  • Thank you. I am now getting this error: Error on line 34: result = highest_turnout(data) # do not change this line! Error on line 20: ans = (county.get_init_voters()/county.get_init_population()) AttributeError: 'County' object has no attribute 'get_init_voters' – Elmer Gustavo Reyes Altamirano May 29 '20 at 13:24
  • is highest_turnout supposed to be part of the class or is it a separate function? – bherbruck May 29 '20 at 13:28
  • @ElmerGustavoReyesAltamirano I updated the answer with the fully indented class and added `highest_turnout()` as a `@staticmethod` so you call it like `County.highest_turnout()`. Do you need all the `get_` methods? you can just do `allegheny.voters` and it will give you the same thing as `allegheny.get_voters()` – bherbruck May 29 '20 at 13:30
  • So I need it so: Expected method 'highest_turnout' to be defined as: def highest_turnout(data): – Elmer Gustavo Reyes Altamirano May 29 '20 at 13:38
  • @ElmerGustavoReyesAltamirano like that? – bherbruck May 29 '20 at 13:40