Why the error message is pointing to the wrong line

Screen Link:

My Code:

def find_sum(a_list):
    a_sum = 0
    for element in a_list
        a_sum += float(element)
    return a_sum
def find_length(a_list):
    length = 0
    for element in a_list:
        length += 1
    return length
def mean(data_set, index):
    column = extract(data_set, index)
    return find_sum(column) / find_length(column)
avg_price = mean(apps_data, 4)
avg_rating = mean(apps_data, 7)
  File "<ipython-input-1-14c330f15539>", line 25
    for element in a_list
SyntaxError: invalid syntax

The error is with the line 17 where “:” is missing in the “for” statement. However the error shows that the line 25 is having the error.

Why do the error message point to the wrong line?

Hi @sumahmud. Someone please correct me if I’m wrong here but I believe the reason for the incorrect lines is due to how the Dataquest coding environment is setup online. I’m not sure but I think when you run your code in the classroom, there are more lines of code being run in the background behind the scenes that we do not see. This is done to ensure variables and files are loaded from previous screens but has the adverse effect of changing the line numbers.

Can you confirm/deny this happens @Bruno ?

What you say is true, but I think something else is the matter here. I don’t think that this led to it pointing to the wrong line.

Does this happen recurrently? @sumahmud

1 Like

This happens to me all the time, for example, in a randomly chosen lesson: Learn data science with Python and R projects where each screen “builds on the previous,” I can generate this behaviour:

Another example: Learn data science with Python and R projects

And one more for good measure: Learn data science with Python and R projects

Whereas with lessons that do not “build on previous screens,” I cannot generate this behaviour, as seen here: Learn data science with Python and R projects

I stand corrected. I also verified that, for instance in your first example, we have the following set up code. It’s seven lines of code, which matches your example perfectly.

happiness2015 = pd.read_csv("World_Happiness_2015.csv")
happiness2016 = pd.read_csv("World_Happiness_2016.csv")
happiness2015['Year'] = 2015
happiness2016['Year'] = 2016
three_2015 = happiness2015[['Country','Happiness Rank','Year']].iloc[2:5]
three_2016 = happiness2016[['Country','Happiness Rank','Year']].iloc[2:5]
1 Like

Nice! Looks like we might have confirmed this behaviour. Out of curiosity, how many lines of set up code are there for the Comparing Frequency Distributions lesson on screen #5 (the last of the three “fail” examples above)?

I ask about this one in particular since it has the largest discrepancy between line numbers so I’m assuming it will have the largest number of “set up lines of code.” (24-25 lines?)

It’s 24 lines of code plus a comment line.

1 Like