# Sal's Shipping

Hi all, I’m requesting for help for the last step of the project. My code seems to have problems with the loop, but I am unsure of how to fix it.

My code is as follows:

``````cost_premium_ground = 125.00

def cost_ground(weight):
cost = 20.00
if weight <=2:
cost += weight * 1.50
elif weight >2 and weight<=6:
cost += weight * 3.00
elif weight > 6 and weight<=10:
cost += weight * 4.00
elif weight >10:
cost += weight * 4.75
return cost

#print (cost_ground(8.4))

def cost_drone(weight):
cost = 0.00
if weight <=2:
cost += weight * 4.50
elif weight >2 and weight<=6:
cost += weight * 9.00
elif weight > 6 and weight<=10:
cost += weight * 12.00
elif weight >10:
cost += weight * 14.25
return cost

#print (cost_drone(1.5))

def find_cheapest(weight):
cost = {}
cost["Drone"]=cost_drone(weight)
cost["Ground"]=cost_ground(weight)
cheapest_cost = ''
minimum = 0
for key in cost:
if cost[key] < minimum:
cheapest_cost = key
minimum = cost[key]
return "The cheapest shipping method is {method} at \${cost}".format(method = cheapest_cost , cost = minimum)

print(find_cheapest(4.8))
print(find_cheapest(41.5))
``````

All help is greatly appreciated!

`return` is nested in the for loop, which means in the first iteration of the loop, the `return` keyword is reached, handing data back to the caller and being done executing (the function that is)

Hello,

I tried nesting the return outside of the loop. While it works for the first example, the second example keeps returning ground shipping as the cheapest method rather than premium shipping. How do I solve this?

Can i see an updated version of your code? I now don’t know what changes you made.

I keep getting this:

``````The cheapest shipping method is  at \$0
``````

which to me, suggest the if condition:

``````if cost[key] < minimum:
``````

has never evaluated to true. Which make sense. `minimum` is 0, while the `cost[key]` are positive numbers. Zero is never greater then the cost of shipping.

Here is the updated code:

``````def find_cheapest(weight):
cost = {}
cost["Drone"]=cost_drone(weight)
cost["Ground"]=cost_ground(weight)
for key in cost:
if cost[key] < minimum:
cheapest_cost = key
minimum = cost[key]
return "The cheapest shipping method is {method} at \${cost}".format(method = key , cost = cost[key])

print(find_cheapest(4.8))
print(find_cheapest(41.5))
``````

here:

``````return "The cheapest shipping method is {method} at \${cost}".format(method = key , cost = cost[key])
``````

`method = key`, `key` will just contain the last value from the loop.

why would you want to print `key`? `key` should only be used in the loop (shame `key` isn’t block scoped)