Learn Python 3/Control Flow/#5

“Sal’s Shipping” link: https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping

I got stuck at first so I decided to use the video tutorial on a step-by-step basis. However, it gives me a NameError that cost is not named.

If you look at the code above, could you guys help me out where/what I did wrong? Thanks! :slight_smile:

Do you define cost within a function? Objects inside a function are local to that function and are forgotten once the function is finished executing. If you wish to use those values in the outer scope where a function was called you’d need to return those values from a function and continue that way.

In this screenshot here I have the tutorial in black and my code in white.

Error is:

Traceback (most recent call last):
File “script.py”, line 47, in
print(“The cheapest option available is $%.2f with %s shipping.” % (cost, method))
NameError: name ‘cost’ is not defined

Oh. I was to keep the indentation differently. Haven’t really grasped that 100% (yet).

It’s working now…

Glad you got sorted. Bear in mind that using print within a function and no return is a less typical way of working. Try and get in the habit of using return and then using print with the returned values as your function will be much more useful that way (e.g. if you update your code in the future). You can have functions which exist with only side effects to print but they’re uncommon.

As with your code sometimes multiple things need to be returned which can be done either with a simple tuple like the example below or by adding values to multiple valued datatype such as a list-

def myfunc(a, b):
    return a, b

value1, value2 = myfunc(1, 1)