I have an assignment to analyze data in csv file for countries and what year they had lowest and highest life expectancy from the Spanish Flu. I keep getting the error “line 21, in
expectancy = int(line[3])
^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ‘27.638\n’”
I have no idea why this keeps coming up so any tips would be great!
lowest = 99999999
highest = -1
low_entity = ""
low_year = ""
high_entity = ""
high_year = ""
input_year = ""
input_year = input("Enter the year of interest: ")
print()
with open("life-expectancy.csv") as file:
next(file)
for line in file:
line = line.split(",")
entity = line[0].strip()
code = line[1]
year = int(line[2])
expectancy = (line[3])
min_country = min(entity)
min_life = min(expectancy)
avg_life = sum(expectancy) / len(expectancy)
#max_life = -1
#min_life = min(expectancy)
#avg_life = sum(expectancy) / len(expectancy)
max_country = ""
max_year = max(year)
min_country = min(entity)
min_year = min(year)
if expectancy > highest:
highest_value = expectancy
max_entity = entity
max_year = year
if expectancy < lowest:
min_value = expectancy
min_entity = entity
min_year = year
#min_life = min(expectancy)
# min_country = min(entity)
if input_year == year:
print(f"For the year {input_year}.\n")
max_life = expectancy
min_life = expectancy
max_country = entity
min_country = entity
print()
print(f"The overall max life expectancy is:{max_life:.2f} from {max_country} in {max_year:.2f}.\n")
print(f"The overall min life expectancy is:{min_life:.2f} from {min_country} in {min_year:.2f}.\n")
print(f"The average life expectancy across all countries was str{avg_life:.2f}\n")
print(f"The max life expectancy was in {max_country} with {max_life:.2f}\n")
print(f"The min life expectancy was in {min_country} with {min_life:.2f}\n")