with open("life-expectancy.csv") as life_file: output = [] year_of_interest = int(input("Please enter your year of interest: ")) year_max = "" year_avg = "" year_min = "" max_expect = 0 for line in life_file: line = line.strip() line = line.split(",") country = line[0] code = line[1] year = int(line[2]) life_expectancy = float(line[3]) output.append([country, code, year, life_expectancy]) min_expectancy = min(output, key=lambda x: x[3]) max_expectancy = max(output, key=lambda x: x[3]) # Where the errors begins print(f"The overall max life expectancy is: {max_expectancy[3]:.2f} from {max_expectancy[0]} in {max_expectancy[2]}") print(f"The overall min life expectancy is: {min_expectancy[3]:.2f} from {min_expectancy[0]} in {min_expectancy[2]}") print() print(f"For the year {year_of_interest}: ") for year_of_interest in line[2]: year_max = max(output, key=lambda x: x[3]) year_min = min(output, key=lambda x: x[3]) year_avg = sum(life_expectancy) / len(life_expectancy) print(f"The average life expectancy across all countries was {year_avg}") print(f"The max life expectancy was in {year_max[0]} with {year_max[3]:.2f}") print(f"The min life expectancy was in {year_min[0]} with {year_min[3]:.2f}")What I want to happen is for a user to enter a specific year and then find the life expectancy information for that year (in a separate csv file). After that I need to display the minimum, maximum, and average of the data on that year. I feel like I'm getting close, but am not quite there. The average either won't compute/show up at all, or saythe 'float' object is not iterable. The year_min and year_max are causing error messages like:print(f"The max life expectancy was in {year_max[0]} with {year_max[3]:.2f}")~~~~~~~~^^^IndexError: string index out of range
What should be outputted is:
Enter the year of interest: 1959
The overall max life expectancy is: 86.751 from Monaco in 2019The overall min life expectancy is: 17.76 from Iceland in 1882
For the year 1959:The average life expectancy across all countries was 54.95The max life expectancy was in Norway with 73.49The min life expectancy was in Mali with 28.077