pythonoptimizationsyntax-errorspaceident

Python: Uexpected an indented block


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!```

Solution

  • 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)]