我需要写一个代码来显示该县的名称:(I)投票率最高的县和(ii)投票的人口百分比。你能帮我一下吗?因为我很困惑。以下是我所做的:
class County:
def __init__(self, init_name, init_population, init_voters) :
self.population = init_population
self.voters = init_voters
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]
def highest_turnout(self):
highest = self[0]
highest_voters = self[0].voters
for county in data:
if county.voters > highest_voters:
highest = county
result = highest_turnout(self)
print(result)
总结来自注释的建议,
highest_turnout
函数需要return
函数highest
,否则在函数完成highest
值后,该函数将丢失。 然后不是传入self
到highest_turnout
,而是传入data
:到目前为止,这将返回并显示“投票率最高的县”(即
allegheny
)的名称。在现在可以创建一个类似的函数来计算投票率最高的县(ii)(在评论中也提到了一种方法)。在
相关问题 更多 >
编程相关推荐