python - Python:Uexpected 一个缩进块
问题描述
我不确定为什么会收到此错误。
我收到此错误:第 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!```
解决方案
缩进不正确,试试:
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)]
推荐阅读
- python - 如何将值从 Tkinter 中的列表框传递到数组,然后将这些值相加?
- docker - Traefik v2:HTTPS 返回 `404 page not found`
- python-3.x - Google Colab Fair NLP ValueError: num_samples 应该是一个正整数值,但得到了 num_samples=0
- android - 为什么 chrome 远程调试在我的三星 Galaxy S10 上不起作用?
- python - Python 与 Azure KeyVault
- php - 替换不属于 HTML 标记的任意字符
- java - 尝试使用 if 语句来确定成绩
- functional-programming - Ramda中带有列表头的管道obj键列表
- javascript - 如何使用 Javascript 将 JSON 响应下载到 Excel?
- reactjs - 嵌套的 Suspense 组件会导致顺序加载还是并行加载?