Sal’s shipping

ground_flatcharge = 20

def ground_shipping(weight):

  if weight > 10 :
    cost = weight * 4.75 + 
    ground_flatcharge
    return cost

  elif weight > 6 and weight <=10 :
    cost = weight * 4 +          
    ground_flatcharge
    return cost
  
  elif weight>2 and weight<=6 :
    cost = weight * 3 + 
    ground_flatcharge
    return cost
  
  else :
    cost = weight * 1.5 + 
    ground_flatcharge
    return cost
  
print(ground_shipping(8.4))

I tried doing it this way but it kept giving me an error. Please help :’(

you can’t just put code on two lines:

    cost = weight * 1.5 + 
    ground_flatcharge

without using parentheses or new escape to a new line

1 Like

Thank you fr pointing that out

1 Like

ground_flatcharge = 20
premium_ground_shipping = 125

def ground_shipping(weight):
if weight<=2 :
final_cost = weight * 1.5 + ground_flatcharge
return final_cost

elif weight <= 6 :
final_cost = weight * 3.0 + ground_flatcharge
return final_cost

elif weight <= 10 :
final_cost = weight * 4.0 + ground_flatcharge
return final_cost

else :
final_cost = weight * 4.75 + ground_flatcharge
return final_cost

def drone_shipping(weight):

if weight<=2 :
final_cost = weight * 4.5 + ground_flatcharge
return final_cost

elif weight <= 6 :
final_cost = weight * 9.0 + ground_flatcharge
return final_cost

elif weight <= 10 :
final_cost = weight * 12.0 + ground_flatcharge
return final_cost

else :
final_cost = weight * 14.25 + ground_flatcharge
return final_cost

def method(weight) :

costground = ground_shipping(weight)
costdrone = drone_shipping(weight)

if costground<costdrone and costground<premium_ground_shipping :
methodused = “ground shipping”
return methodused

elif costdrone<costground and costdrone<premium_ground_shipping :
methodused = “drone shipping”
return methodused

else:
methodused = “premium ground shipping”
return methodused

def get_cheapest_shipping(weight):

costground = ground_shipping(weight)
costdrone = drone_shipping(weight)

if costground<costdrone and costground<premium_ground_shipping :
final_cost = costground
return final_cost

elif costdrone<costground and costdrone<premium_ground_shipping :
final_cost = costdrone
return final_cost

else :
final_cost = premium_ground_shipping
return final_cost

final_cost = get_cheapest_shipping(41.5)
methodused = method(41.5)
print("You should use "+ methodused + " and it cost " + str(final_cost))

I did it this way, it works now but I would need to call the two functions separately for get_cheapest_shipping as well as method

Is there a way of calling using one function but getting both cheapest cost and method of shipping

There is. However, return can only hand back a single thing. However, this thing can be a container (like list or dictionary) containing multiple values.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.