如何创建遍历每个县的函数,计算投票率?你知道吗
class County:
def __init__(self, init_name, init_population, init_voters) :
self.name = init_name
self.population = init_population
self.voters = init_voters
def highest_turnout(data) :
100 * (self.voters / self.population)
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]
类
County
定义正确。 但是,函数county
不正确。你知道吗在函数
highest_turnout
中传递数据时,必须首先计算列表中第一个County
的投票者百分比-它位于data[0]
。 然后我们将“highest”设置为第一个County
的国家名称,我们假设data
列表中的第一个是我们见过的最高的国家名称。你知道吗接下来,我们使用
for
循环开始迭代列表data
中的所有County
对象,以便传入每个County
对象。你知道吗变量
pct
给出了在当前步骤中运行的County
中投票者的百分比。if
函数将其与存储在变量pct
中的最高百分比进行比较。如果新的百分比高于pct
(返回True),则更新最高的百分比变量pct
,从而更新县名称。你知道吗相关问题 更多 >
编程相关推荐