Sal's Shipping

Hello,
Could you, please, help me understand what is happening with my code below?
I could not find out the cause of the problem. Thanks

def ground_shipping(weight):
if weight <= 2:
return weight * 1.50 + 20.00
elif weight <= 6:
return weight * 3.00 + 20.00
elif weight <= 10:
return weight * 4.00 + 20.00
else:
return weight * 4.75 + 20.00

print(ground_shipping(8.4))

premium_shipping = 125.00

def drone_shipping(weight):
if weight <= 2:
return weight * 4.50
elif weight <= 6:
return weight * 9.00
elif weight <= 10:
return weight * 12.00
else:
return 14.25

print(drone_shipping(1.5))

def cheapest_shipping(weight):
ground = ground_shipping(weight)
drone = drone_shipping(weight)
premium = premium_shipping
if ground < drone and ground < premium:
method = “Ground_shipping”
cost = ground
elif premium < ground and premium < drone:
method = “Premium_shipping”
cost = premium
else:
method = “Drone_shipping”
cost = drone

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

Hey there @cferreira264!! Welcome to the Codecademy forums :grinning:

It is kind of hard to tell since your code in not properly formatted, so there is no indentation.

The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

Press Enter to go to a blank line, click that icon, and you will see this:

Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into an IDE for testing.


Based on how it looks though, I would say it is because there is no return keyword in your cheapest_shipping function, so cost is never returned back to the main program. At the same time this could be wrong since I can’t see your indentation. If you could paste your code formatted that would be very helpful.

def ground_shipping(weight):

if weight <= 2:

return weight * 1.50 + 20.00 

elif weight <= 6:

return weight * 3.00 + 20.00

elif weight <= 10:

return weight * 4.00 + 20.00

else:

return weight * 4.75 + 20.00 

print(ground_shipping(8.4))

premium_shipping = 125.00

def drone_shipping(weight):

if weight <= 2:

return weight * 4.50

elif weight <= 6:

return weight * 9.00

elif weight <= 10:

return weight * 12.00

else:

return 14.25

print(drone_shipping(1.5))

def cheapest_shipping(weight):

ground = ground_shipping(weight)

drone = drone_shipping(weight)

premium = premium_shipping

if ground < drone and ground < premium:

method = "Ground_shipping"

cost = ground

elif premium < ground and premium < drone:

method = "Premium_shipping"

cost = premium

else:

method = "Drone_shipping"

cost = drone 

print(

“The cheapest option available is $%.2f with %s.”

% (cost, method)

)Preformatted text

53.6
6.75
Traceback (most recent call last):
File “script.py”, line 43, in
% (cost, method)
NameError: name ‘cost’ is not defined

Thank you, but that is only partly formatted. You have to put all of your code inside one set of back ticks.

I would still guess it is because your cheapest_shipping function has no return statement. Is your last print() part of the function or outside it?

I fixed the last print indentation and it worked! Thanky you very much for your help!!

1 Like