Python类和对象分配

2024-09-30 01:23:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要编写一个执行以下操作的程序: 首先,找出投票率最高的县,即投票率最高的县 使用对象的人口和选民属性投票的人口 然后,返回一个元组,其中包含投票率最高的县的名称和 按此顺序投票的人口百分比;百分比应为 表示为介于0和1之间的数字

我尝试了一下,但得到了以下错误:

第19行错误: 阿勒格尼=县(“阿勒格尼”,1000490645469) TypeError:对象()不接受任何参数

这是我到目前为止所做的。非常感谢你的帮助

class County:
  def __innit__(self, innit_name, innit_population, innit_voters) :
   self.name = innit_name
   self.population = innit_population
   self.voters = innit_voters
    
def highest_turnout(data) :
  highest_turnout = data[0]
  for County in data:
    if (county.voters / county.population) > (highest_turnout.voters / highest_turnout.population):
      highest_turnout = county

  return highest_turnout

# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
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!

Tags: nameselfdata人口populationcountythesehighest

热门问题