Oct-20-2024, 07:16 PM
have some more problems with this code but i thing if i can overcome this one i can find a way to fix the others as well. have a file with country, year, and life_expectancy and i need to calculate what is the average life_expectancy from all the country put together from a choosen year. so the user types in a year and the programm print him the average number from that year. here is the parth where my code struggles the most.
if choose_year.lower() == year: year_expectancy = sum(expectancy) / len(expectancy)i can not use panda or other libarys and should be only fixed with relly basic coding. thank you all for your help
#Ah example of how the list work # germany, ger, 2021, 20.663, # germany, ger, 1990, 17.638, # brasil, bra, 1999, 22.473, # brasil, bra, 2002, 7.982, # England, UK, 2021, 9.827, # england UK, 1999, 14.672, # japan, ja, 2005, 20.661, # japan, ja, 2021, 16.836, # mexico, mx, 2008, 11.383, # mexico, mx, 1999, 26.837, life_expectancy = open('D://life-expectancy.csv') with open('D://life-expectancy.csv') as data: max_expectancy = 0 min_expectancy = 99999999 index = 0 choose_year = 0 year_expectancy = 0 for line in data: data = line.strip() data = line.split(',') entity = data[0] code = data[1] year = int(data[2]) expectancy = float(data[3]) if expectancy > max_expectancy: max_expectancy = expectancy max_country = entity max_year = year if expectancy < min_expectancy: min_expectancy =expectancy min_country = entity min_year = year choose_year = input('Enter the year of interest : ') # prints allways 0. that calculation does not work if choose_year.lower() == year: year_expectancy = sum(expectancy) / len(expectancy) print() print(f'The overall max life expectancy is: {max_expectancy} from {max_country} in {max_year}') print(f'The overall min life expectancy is: {min_expectancy} from {min_country} in {min_year}') print() print(f'For the year {choose_year}:') # here i need the answer of the calculation put it print(f'The average life expectancy across all countries was {year_expectancy}') # #print(f'The max life expenctancy was in {} with {}') # #print(f'The min life expectancy was in {} with{}')