Home > Net >  assistance needed please, float is not iterable issues
assistance needed please, float is not iterable issues

Time:12-03

`

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

`

I'm having some troubles in finding the average life expectancy given a specified year, it returns with a 'float' not iterable error and I'm pretty lost.

CodePudding user response:

A bit confusing answering this without your input and what line throws the error, but I'm guessing it's due to the 'sum(life)' - life seems to be a float while sum expects an iterable

CodePudding user response:

sum needs a list of values to add to each other ;)

  • Related