Sal's shipping control flow task https://www.codecademy.com/paths/data-science/tracks/dspath-functions-and-logic/modules/dspath-control-flow/projects/sals-shipping

Hi im attempting this taks and have written the following code but i keep getting the error: File “script.py”, line 31
return print("Ground shipping is cheapest, and costs £ "+ ground)
^
SyntaxError: ‘return’ outside function

this is my code:

def what_shipping_method(weight):
ground=cost_groundshipping(weight)
drone=cost_droneshipping(weight)
premium=cost_p_groundshipping

if ground< drone and ground< premium:
return print("Ground shipping is cheapest, and costs £ "+ ground)
if drone< ground and drone< premium:
return print(“Drone shipping is cheapest, and costs £”+ drone)
if premium< drone and premium< ground:
return print(“Premium ground shipping is cheapest, and costs £”+ premium)

any help is much appreciated :slight_smile:
ps. all the return statements are indented by one space but i can’t get it to stay like that in this question :slight_smile:

Have a gander at this FAQ which covers the basics of formatting your code.

The error itself is fairly straightforward. A return statement is used outside a function definition. It sounds like a problem with idnentation. At some point you’ve left the function definition and consequently when you run the code an error is thrown.

Here’s an example of indentation with a function defintion with an if statement followed by a return (I bumped it up to four space indents to make it clearer, you may as well stick to codecademy’s two spaces for the lessons but it should be consistent.)

def a_function(num):
    print(num)
    # some other statements...
    if num < 10:
        return "Input was less than 10"
    if num >= 10:
        return "Input was greater than or equal to 10"
3 Likes

I’ll chime in and say that sometimes I have trouble getting my indentations flush if there’s a lot of code on Codecademy. My solution there is to move it to a text editor (like atom or sublime) and use their smart indentation correction tools. Works every time.

You should however, know how to indent properly in python so try to use it as a last resource!

3 Likes

i want help on where i went wrong untill my code throws incorrect results

corrected it with an and

Hello everyone.I am receiving an error

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

I followed every step as in the solution, however, it seems that cost is not defined?

def shipping_cost_ground(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 (shipping_cost_ground(8.4))

shipping_cost_premium = 125.00

def shipping_cost_drone(weight):
if weight <= 2:
price_per_pound = 4.50
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 print_cheapest_shipping_method(weight):

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

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

if ground < premium and ground < premium:
method = “ground method”
cost = ground

else:
method = “premium method”
cost = premium

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

% (cost,method)

)

It’s very hard to tell without formatting and indentation, please see the following FAQ which covers how you might add this.

On a side note are you sure about your control flow order in the second function? Make sure it works as you expect.

https://gist.github.com/7c1ac48a8c539037a2d47e1de6133ac2

premium_ground_shipping = 125.00
#weight = 8.4

#Ground shipping
def ground_cost(weight):
if weight <= 2.0:
print("Ground shipping: " + str(weight) + "kg costs " + “" + str(weight * 1.50 + 20.00)) elif weight <= 6: print("Ground shipping: " + str(weight) + "kg costs " + "”+ str(weight * 3.00 + 20.00))
elif weight <= 10:
print("Ground shipping: " + str(weight) + "kg costs " + “"+ str(weight * 4.00 + 20.00)) elif weight > 10: print("Ground shipping: " + str(weight) + "kg costs " + "”+ str(weight * 4.75 + 20.00))

#Drone Shipping
def drone_cost(weight):
if weight <= 2.0:
print("Drone shipping: " + str(weight) + "kg costs " + “"+ str(weight * 4.50)) elif weight <= 6: print("Drone shipping: " + str(weight) + "kg costs " + "” + str(weight * 9.00))
elif weight <= 10:
print("Drone shipping: " + str(weight) + "kg costs " + “"+ str(weight * 12.00)) elif weight > 10: print("Drone shipping: " + str(weight) + "kg costs " + "”+ str(weight * 14.25))

#calculate cost of ground shipping based on weight input()
ground_cost(41.5)

#Ground Shipping Premium flat charge
print("Ground Shipping Premium: " + “$” + str(premium_ground_shipping), “flat rate”)

#calculate cost of drone shipping based on weight input()
drone_cost(41.5)