C3 W2 problem cost values not matching expected

My code compares to what is given in the hints, but I get the wron cost value. 1.71 vs 13.67 Can someone look at my code and help me, please.

The error shows that the output of your code does not match the expected cost value.

Please cross-check your code against the hints one more time.

I’m not finding the error.

You have not added the regularized cost.

Please do not post your solution code on the forums. This is not allowed by the code of conduct

When I review the text above Example 1, the regularization formula is included on the same line as the rest of the cost function formula, so I’m confused as to why I should put the regularization term after the J/2 line. Additionally the lambda is zero in the first calculation, hence I don’t understand why my cost funciontion calculation is off, even if the regularization term is misplaced. When I review the hints, my program matches.

It is up to you to add the regularized cost to the variable J on the same line of code or below it. The output will be the same.

You have an indentation problem in this line of code.

Somehow, I manage to fix the indentation problem and got the right result for the first part of t he problem yeah! The regularization part was still off. After playing around with the code some more —now I’m getting a syntax error, invalid syntax on the line J=J/2 ! What is wrong with J = J/2 ? Very frustrating. I’m lost.

It appears that you may still have improper indentations.

These errors are quite fundamental, and if you’re unable to resolve them, I recommend starting with a Python course as a first step.

I can’t show you my code here, all I can say is that you need to make sure J= J/2 needs to be aligned (vertically) with the first ‘f’ in ‘for’ (just below the first #) in the code, and also that when you put the regularisation code, it’s also aligned with ‘f’ in for.


Check this out for some common causes for Invalid Syntax . I suspect there were some missing parentheses/brackets in the line previous to the one that triggers the error.


I was missing a parenthesis in the previous line. Sometimes it’s really hard to find the mistakes in your own code, like missing parenthesis. Now I nedd to get the numbers to match with regularization.

Thank you for your assistance, greatly appreciated.

A lot of the time indeed :wink:

However, parenthesis/brackets check becomes my reflexes and if someone asked me why, it was because “a lot of the time”. :wink:

Although I don’t know you, from what you have said that it had been some hard time, I think it is the right track!


I really scrutinized the alignments you suggested, it seems that a missing parenthesis was th culprit. I put the regularization term in the same line, like they do in the formula in the text above. formula (1) My cost function with regularization is off 159.9 vs the desired 28.09…back to the salt mine!

Thank you for your assistance, always appreiated.

For me the code worked when the alignment was as I described previously here. I was also getting your high numbers previously–but it worked, after spending some time rearranging the code for alignment issues. I’m wondering though in the real world, how will you spot, the error answer when no one gives that to you?