AssertionError in the Week2 practice lab: linear regression

Hello everyone. I have an assertion error (in the first programming assignment, week 2, course 1) and I tried to figure out the problem (even reviewing previous answers in the community). But still I cannot solve the issue.

1 Like


This is your first post ! Welcome to the community!

Regarding your error, please make sure that the indentation inside the for-loop is ok for all lines. Python needs a proper indentation of instructions that go under the for.

Also: Please delete your code as it goes against the Code of Conduct to publish code in this space.

Thank you!


1 Like


Your cost_sum is done outside of the for loop, so the cost is changed every time it loops round the loop. The cost_sum should be inside the for loop.

1 Like

I think your code is not indented correctly.
Python uses indentation levels to organize blocks of code.

1 Like

Thank you. I am trying to delete it. Sorry

Thank you very much!

1 Like

I will remove the code from the replies.

1 Like

curiously I get the right answer = “Cost at initial w: 75.203”

but get the folowing error:
AssertionError: Case 1: Cost must be 0 for a perfect prediction but got 12.134178884999997

Thank you very much every. I corrected the indentation and the problem was solved. I enjoyed the exercise!

1 Like

I can’t see any errors in my indentation in my for loop: everything is indented one tab setting, my final line “total_cost = cost_sum/(2*m)” is aligned with “for” That’s the way class examples write the code?

It doesnt have to be aligned with the For line, but need to be indented, which means aligned with what is inside the For loop (indented with respect to For)

I know my explanation is not really clear. I just have a very small experience in programming

My output:

<class ‘numpy.float64’>
Cost at initial w: 75.203

AssertionError Traceback (most recent call last)
9 # Public tests
10 from public_tests import *
—> 11 compute_cost_test(compute_cost)

~/work/ in compute_cost_test(target)
9 initial_b = 3.0
10 cost = target(x, y, initial_w, initial_b)
—> 11 assert cost == 0, f"Case 1: Cost must be 0 for a perfect prediction but got {cost}"
13 # Case 2

AssertionError: Case 1: Cost must be 0 for a perfect prediction but got 12.134178884999997

I believe python relies on tabs to be expanded into a fixed number of spaces. Four is pretty typical.

The entire code inside the function must be indented from column 1, otherwise Python thinks you have already ended the function.