'Display the input from from the csv

    with open("life-expectancy.csv") as life_expectancy:
    next(life_expectancy)

    output = []

    years = input('Enter the year of interest: ')

    for data in life_expectancy:
        clean_data = data.strip()
        split_data = clean_data.split(',')

        entity  = split_data[0]
        code = split_data[1]
        year = split_data[2]
        expectancy  = float(split_data[3])
      
        ## Append to the list
        output.append([entity, code, year, expectancy ])

max = max(output, key=lambda x: x[3])
min = min(output, key=lambda x: x[3])

print('')
print(f'The overall max life expectancy is {max[3]:.2f} from {max[0]} in {max[2]}')
print(f'The overall max life expectancy is {min[3]:.2f} from {min[0]} in {min[2]}')
print('')

<--------- I don't know what to do for this part!----------->

print('For the year {years}}:')
print('The average life expectancy across all countries was 54.95')
print('The max life expectancy was in Norway with 73.49')
print('The min life expectancy was in Mali with 28.077')

What I wanted to do is when a user inputs a year; that input must display the year's life-expectancy from maximum, average and minimum, but my problem is that I don't know how to do it. Please help! Thank you!



Solution 1:[1]

You can try checking if user input is in your csv file(in a column) or not with "if .. in ..." command, then try matching the informations.

if years in ....:
    

p.s: print('For the year {years}}:') there is an extra '}' at the end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 romancedawn