Sal's Shipping name error

Hello, I am stuck in this project even though, I followed all the steps from the help video and pretty much wrote the code in the exact way they show on the video, however, it still gives me a name error and I don’t know what to do anymore as the code has been written with no errors.
This is the error it appears: Traceback (most recent call last):
File “script.py”, line 54, in
print_cheapest_shipping_method(4.8)
NameError: name ‘print_cheapest_shipping_method’ is not defined

My code is written below, let me know if you stop any error, please!

def ground_shipping(weight):

if weight <= 2:
price_per_pound = 1.50
elif weight <= 6:
price_per_pound = 3.00
elif weight <= 10:
price_per_pound = 4.00
else:
price_per_pound = 4.75

return 20 + (price_per_pound * weight)

print(ground_shipping(8.4))

shipping_cost_premium = 125.00

def shipping_cost_drone(weight):

if weight <= 2:
price_per_pound = 4.00
elif weight <= 6:
price_per_pound = 9.00
elif weight <= 10:
price_per_pound = 12.00
else:
price_per_pound = 14.25

return price_per_pound * weight

print(shipping_cost_drone(1.5))

def cheapest_shipping(weight):

ground = shipping_cost_shipping(weight)
premium = shipping_cost_premium
drone = shipping_cost_drone(weight)

if ground < premium and ground < drone:
method = “standard ground”
cost = ground
elif premium < ground and premium < drone:
method = “premium ground”
cost = premium
else:
method = “drone”
cost= drone

print(
“The cheapest option available is $%.2f with %s shipping.”
% (cost, method)
)

print_cheapest_shipping_method(4.8)
print_cheapest_shipping_method(41.5)

sorry the link for the code is the following:

Where is that method to be found?

what do you mean? I dont understand, I just wrote all the code just like the guy on the help video wrote it but i keep getting that error

Yes, but is there a method called, print_cheapest_shipping_method()?

yes, there is as that’s what’s asking me to write in step 7 of the project.

What line number is it on? (The function, that is; sorry, it is not a method).

>>> dir()
[..., 'cheapest_shipping_method', 'ground_shipping', 'shipping_cost_drone', 'shipping_cost_premium']
>>> 

According to that there is no such function in the namespace.

the function is located at line 33

That is what I thought you would say. Is that the same as,

print_cheapest_shipping_method

?

The names have to match.

if there is no function, what am i supposed to do to get the result from the project?
I’ve written all the code exactly the same as the help video, so i would of thought everything is fine

You are calling it with the wrong name. Call it with the correct name and see what happens.


Aside

So, let me ask you, since you just did what was in the video, did you learn anything that you can take away from this exercise?

No, I have to say it’s very complicated for beginners like myself, however, I do understand half of it, it’s about practicing more, and then I’ll understand it better i think

what am i calling the wrong name? the function or the print at line 53?

Line 53 calls a function that does not exist. Just affixing a print_ prefix does not have the effect of printing. You have to call the function by the correct name.

That means it is time to put the brakes on and go back to the beginning of the Python introduction and review everything you have taken up so far. Do the practices, and during review take the time to read the docs on any new keywords. Practice in the workspace (after saving your original work in a text editor) with variations on the concept and play with each concept a bit before rushing on. If you skip this step it will only continue to get harder and harder as you move ahead. Learning means practice, and lots of it. We cannot get past that critical requirement.

1 Like

but that function is to print the code written above the function, no? i am confused with this and i dont seem to find the error yet

def cheapest_shipping_method(weight):

That’s the function signature at line 33.

print_cheapest_shipping_method(4.8)
print_cheapest_shipping_method(41.5)

Those are the function calls at lines 52 and 53. Do the names look the same? print_ is part of the name, not a command.

i get you, so would this the right command?

print(cheapest_shipping_method(4.8))
print(cheapest_shipping_method(41.5))

1 Like

Yes, but only if you change line 49 to a return statement, not a print statement; otherwise, you will be printing the return from print()None.


Small correction in terminology. In program code, that is a statement. It is only a command when it is given directly through the interactive console…

>>> print (cheapest_shipping_method(41.5))
The cheapest option available is $125.00 with Premium shipping.
>>>

I just fudged a response but what we see are the command prompt, >>> of the interactive shell. When you install Python on your machine you will see that. It means we can type in any Python command and it will execute immediately.

ive done this already:
return “The cheapest option available is $%.2f with %s shipping.”

however, i still get a error in line 50