`
life_max = -5
life_min = 999
country_max = ""
country_min = ""
answer = int(input("Which year would you like to enter? "))
with open ("life.csv") as f:
next(f)
for line in f:
parts = line.split(",")
life = float(parts[3])
year = int(parts[2])
country = parts[0].strip()
code = parts[1].strip()
if life > life_max:
life_max = life
country_max = country
if life < life_min:
life_min = life
country_min = country
average = range(sum(life)) / range(len(life))
print(f"The average is {average}")
print(f"The country with the worst life expectancy is {country_min} at {life_min} years.")
print(f"The country with the best life expectancy is {country_max} at {life_max} years.")
`
我有一些麻烦,在寻找平均预期寿命给定的一年,它返回一个'浮动'不可迭代的错误,我相当迷失。
2条答案
按热度按时间h79rfbju1#
在没有输入的情况下回答这个问题有点混乱,是哪一行抛出了错误,但我猜这是由于“sum(life)”- life似乎是一个浮点数,而sum预期是一个可迭代的
nbysray52#
sum需要一个值列表来彼此相加;)