-3

我不确定为什么会收到此错误。

我收到此错误:第 24 行错误:tempmax = ans ^ IndentationError: expected an indented block

这是我到目前为止所拥有的:

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

1 回答 1

0

缩进不正确,试试:

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

输出:

[('chester', 0.7215045058280377)]
于 2020-05-29T13:10:58.403 回答