Sals Shipping Unbound Local Error

Hey, I keep getting:

UnboundLocalError: local variable ‘method’ referenced before assignment

For the assignment:
https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-control-flow/projects/sals-shipping

I even copied the solution code in the video…

Here’s my code:

Your error message say which variable it is, and says what’s wrong (it isn’t bound when used)

If you Ctrl+F for that variable then there aren’t all that many occurrences of it that you would need to consider

Line 40

method: "premium ground"

Hey mtf,

Not sure I follow…

Can you elaborate?

Appreciate the help

Line 40 is making an attribute assignment, all the others are direct value assignments using =.

1 Like

Wow, good eye. Thanks for your help

1 Like

Maybe. Because the code is short enough to make that a possibility.

But you can get there from reading the error message. It says which variable (method). It says where it happens (line number). It says what happened (variable not assigned before use) You can now look at where you think you defined it, and now you’d be looking at a single line where the problem is.

That’s what you need to be doing a lot of the time. You’ll make mistakes. Silly ones. Small ones. But if you follow up on what’s going wrong, then you can locate it without a need for a magical eye.

1 Like

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. To modify a global variable inside a function, you must use the global keyword.