python-3.x 需要帮助,请,浮动是不可迭代的问题

0s0u357o  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(175)

`

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.")

`
我有一些麻烦,在寻找平均预期寿命给定的一年,它返回一个'浮动'不可迭代的错误,我相当迷失。

h79rfbju

h79rfbju1#

在没有输入的情况下回答这个问题有点混乱,是哪一行抛出了错误,但我猜这是由于“sum(life)”- life似乎是一个浮点数,而sum预期是一个可迭代的

nbysray5

nbysray52#

sum需要一个值列表来彼此相加;)

相关问题