Ex 1 - Compute Cost - NameError?

For my efforts when I check if my implementation is correct I get the following message

NameError: name ‘total_cost’ is not defined

What am I doing wrong? Any quick tips would be helpful.


My code is below:

{removed by mentor}

Hi @Anthony_Prangley,

We still need to see the full Trackback - the full error message that Jupyter returned to you at the error.

Could you please share a screenshot of that?


PS: I have to remove the code for you since it’s not allowed to share assignment work here publicly. :wink:

Here is the full track back message. Thanks for the help!

Hi @Anthony_Prangley,

That NameError is a result of calling a name (of a function, or a variable, for example) that has not been defined yet. total_cost should be a variable, and is defined by

  • assigning a value to it, plus
  • running successfully the code cell that defines it

My suggestion is for you to observe that point number 1 has been done, within the function compute_cost, and then run that code cell again - make sure no error is returned from running it, before attempting the test once more and see if the error is gone.


Thanks for the quick response. I appreciate your help!
I am a bit confused but let me try figure it out. I will post again once I have worked at it…

Sure. The symptom of the error is simple - just not yet defined a name in the eye of Python (not us). However, the cause of it can be complicated - maybe it was just the ordering of running the code cell being edited, but this one is easy to resolve by running the code cells successfully for one more time in the expected order.

Let us know.

I would guess that inside your compute_cost() function, you did not set a value for the “total_cost” variable.

I have now defined total_cost by a line of code
total_cost = 0.
From my understanding this line of code defined total_cost and Python now knows it exists. I then re-ran the code.

But now I get this feedback that I have a NameError for compute_cost not being defined.

At the beginning I have this code line.

def compute_cost(x, y, w, b):

Does that not define compute_cost?

I am stuck again! :face_with_head_bandage:

It means you need to re-run the cell that defines that function.

Every time you open a notebook, you need to run all of the cells starting from the top.

Thank you. Are you saying I need to go back and run the code for the previous sections. Is that correct? I tried that

And I have a new error message :slight_smile: So that is progress :slightly_smiling_face:

Can you advise on what is an UnboundLocalError?

I did not know, so I did a quick internet search.

I found that it means you have a local variable (inside a function), which you did not assign a value to before your code tried to use the variable.