Graded lab 1 - error messages in training cells

I’m getting error messages on the training cells, where we are just supposed to run the code as written. In cell seven, an error message says compute_cost is not defined, but it clearly is in previous cells.

Any suggestions?

Error 1:
image

Error 2:
image

You have to run the cells in order for the functions to actually be defined. Just having the code sitting there does nothing, until you run the cell that contains the definition of the compute_cost function.

Also note that the indentation is a key part of the syntax of python: it determines the structure of the logic. You can’t just mess with the indentation to make the code “look better” to your eyes. It looks like you accidentally or purposely removed the indentation on that return statement so that it no longer belongs to the function body.

If you want to get a clean copy of the assignment to compare and repair that type of damage, the first topic on the DLS FAQ Thread shows how to do that.

If your first block of code, the “return” statement is not indented correctly.

1 Like

Also please avoid posting your code on the forum. That’s not allowed by the Code of Conduct.

Sorry, I didn’t know that. Won’t happen again.

The error message is the RESULT of running the cell. I know you have to run the cell.

The indentation is an error. I find your response a little derogative. The tone is dismissive and rude. If you have had aggressive responses to previous help, I am sorry for that, but I was not the aggressor.

Hi, Andrea.

I apologize if you found the tone of my response off putting. What I was intending to do was simply state facts. Sorry if it came off badly.

I’m not actually a mentor for MLS, but was just trying to help as no-one else had answered at the time I responded. Perhaps I’d best just let the actual MLS mentors respond.

Hello @AndreaH1,

image

For this error, it means that the code cell that contains the definition for compute_cost has not been executed successfully. It is obviously not successful because the execution of that cell resulted in the following error:

image

which basically complained that you have put the return statement outside of a function. For it to be inside a function, we need proper indentation. You might google for more about indentation, but in short, we group compound statements under the same indentation level. Since we may need many lines of code to define one function, we need to put those lines under the same indentation level for Python to recognize them as belonging to the same function. In your case, the return line should have been indented once to fall within a part of the compound statements for the def that defines compute_cost, but apparently it had not been indented.

I hope my message can get you some ideas of where the problem lies, but if you have no idea what I am talking about, you probably will need to google for more or even take a fundamental Python tutorial or course.

If you fix the return error, and successfully execute the cell that defines compute_cost, then both errors that you have reported should go away. If you experience new error, try google for ideas and solve it yourself, and if you can’t solve it, please open a new thread with only the error message (like how I have edited your first post of this thread to)

Good luck, keep learning, and cheers,
Raymond

Paul, I appreciate the help and it’s clear you’ve gotten some hostile responses. I’m sorry for that, and I’m kind of embarrassed it was an indentation error (doh!). Thanks for responding, and please consider assisting others. Helping each other is what makes the world go round.

I fixed the indentation problem, but when I go to run cell 13, which tests implementation, I get an error message. Please note that cell 13 contains absolutely no input from me. It’s 100% instructor generated. I’ve run every cell again, and it makes no difference.

Any thoughts?


The error says that in line 28, your compute_cost() function uses the “cost” variable before it is defined.

It’s not clear to me why you have “cost = cost + …”, in that line, when in the next line, you add cost to cost_sum.

It’s not my code. Its 100% instructor generated. I made no changes. The instructions are simply to run it to test implementation. Trust me, I don’t understand this either.

Oh wait a second, could that be why? I am asked to keep track of all costs to generate total cost, so I initialized with cost_sum. We are not supposed to post any of our code here (I didn’t know that until I was informed, so I apologize for that). Perhaps introducing a new term to collect the sum of all costs is causing the problem?

Cell 13 is a test that runs your code for compute_cost().
compute_cost() is in Cell 12.
Your code there has an error.

It’s the cost_sum term, I think.

Thank you.