Python: 预期一个缩进的块[重复]

问题描述 投票:-2回答:2

我不知道为什么我得到这个错误。

我得到了这个错误:Error on line 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!```
python optimization syntax-error space ident
2个回答
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)]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.