Reputation: 115
For an assignment Im working on I need to go through a file from OurWorldInData.org which has info on all countries names, codes, years and life expectancy's with spanish flu and I need to pull out the year and country name that had the lowest and highest life expectancy as well as offer a way for the user to enter a year and the program to display the life expectancy from that year. I don't know how much help yall will be able to offer since its kinda hard to emulate if you don't have the csv file of the info but this is my code right now.
filename = 'Life_expectancy.csv'
with open(filename) as le_file:
next(le_file)
for line in le_file:
line = line.strip().split(",")
name = line[0]
code = line[1]
year = int(line[2])
life_expectancy = float(line[3])
min(life_expectancy) = min_life_expectancy
print(f'Name: {name}, Code: {code}, Year: {year}, Life Expectancy: {life_expectancy}')
I struggled for a really long time trying to wrap my head around this assignment and decided just to try min(life_expectancy), but got an error that reads "SyntaxError: can't assign to function call" and I don't know what this means. If anybody could help me I would greatly appreciate it.
Upvotes: 0
Views: 36
Reputation: 171
So I created a wee dummy data file called life.csv
. Easy enough. It looks like this:
Name1,asd,1990,74,blah,blah,blah
Name2,bec,1923,41,blah,blah,blah
Name3,fre,1983,93,blah,blah,blah
Name4,jve,1066,23,blah,blah,blah
Name5,vne,1930,43,blah,blah,blah
A good bit of advice is to create a dummy for asking questions, but don't worry about that for the moment! :-) Hopefully it looks a little like the top part of yours!
I've revised your code. If you're having trouble diagnosing this type of loop remember that having print() statements in each iteration is your friend when debugging, as you start out! However, I'm hoping I've not been too abstract so you're happy to work through it and understand what's going on:
rows = []
with open(filename) as le_file:
next(le_file)
for line in le_file:
line = line.strip().split(",")
rows.append(line)
With this you'll end up with a variable called rows containing your split data.
rows
[['Name1', 'asd', '1990', '74', 'blah', 'blah', 'blah'],
['Name2', 'bec', '1923', '41', 'blah', 'blah', 'blah'],
['Name3', 'fre', '1983', '93', 'blah', 'blah', 'blah'],
['Name4', 'jve', '1066', '23', 'blah', 'blah', 'blah'],
['Name5', 'vne', '1930', '43', 'blah', 'blah', 'blah']]
Now we can extract the INDEX of the row that has the smallest age like so:
ages = [row[3] for row in rows]
min_age_idx = ages.index(min(ages))
This returns the first index in the CSV of the minimum age, which you can now use to get the details of the entry in question:
details = rows[min_age_idx]
print('Name: {}, Code: {}, Year: {}, Life Expectancy: {}'.format(details[0], details[1], details[2], details[3]))
Now, I appreciate this isn't the most concise solution but very illustrative of what you're trying to achieve. The problem you were having is that you were still reading through the file assigning entries to variables, BEFORE you had any idea which line has the minimum age. Therefore each age AFTER the line with the minimum would reassign the variables you eventually use in your print statement. There are many ways to solve the problem or simplify the print statement, including using help libraries like pandas to read the CSV but I think it important to know what's going on with your problem!
The reason for "can't assign to function call" is because min() is a function call and it's on the left hand side of your statement. If you wanted to assign a variable with the result of a min() function call, it would look like:
my_min_value = min(values)
The left hand side of the =
is what you're assigning to, the right hand side is the operation whose result you want to assign to it.
I really hope this helps clear things up for you! :-)
Upvotes: 1