我不确定为什么会出现此错误。
我收到此错误: 第 24 行错误: 最大温度 = ans ^ 缩进错误:预期为缩进块
这是我到目前为止所拥有的:
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!```
缩进不正确,请尝试:
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)]