# Not understanding how to write this function that compares results to two other functions

I’m having a tough time understanding how to write the function in step 6. Should I take the results of the two functions I created earlier and then compare? Not really sure an efficient way to write this. Here is what I have:

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

print(cost_of_ground_shipping(8.4))

cost_of_prem = 125.00

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

print(cost_of_drone(1.5))

def cheap(weight):
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "ground is cheaper"
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "drone is cheaper"
if cost_of_ground_shipping(weight) or cost_of_drone(weight) > cost_of_prem:

print(cheap(41.5))
``````
1 Like
``````if cost_of_ground_shipping(weight) or cost_of_drone(weight) > cost_of_prem
``````

this is saying

• if cost_of_ground_shipping(weight) exists (is not None)
OR
• cost_of_drone(weight) > cost_of_prem

``````1 or 5 < 6
#Output:
# 1
5 < 6 or 1
# True
1 < 6 or 5 < 6
# True
``````
2 Likes

Thank you that makes a lot more sense and helps me recognize my mistake. However I adjusted it to be more explicit and I’m still not getting the right results.

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

#print(cost_of_ground_shipping(41.5))

cost_of_prem = 125.00

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

#print(cost_of_drone(41.5))

def cheap(weight):
if cost_of_ground_shipping(weight) < cost_of_drone(weight):
return "ground is cheaper"
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) or (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"

print(cheap(41.5))
``````
1 Like

Drop a few print statements to see what your values you are putting in the cheap() function. It may highlight for you where the logic is breaking.

1 Like

Any value I put it’s bringing back “ground is cheaper”

One question I have, is it taking that weight value in my cheap() function and applying it to my first two functions?

1 Like
``````def cheap(weight):
if cost_of_ground_shipping(weight) < cost_of_drone(weight):
return "ground is cheaper"
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) or (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"
print(cheap(41.5))
``````
• it will run cheap with a weight of 41.5
• it will evaluate cost_of_ground_shipping(41.5) < cost_of_drone(41.5)
• etc

That’s why before the if condition runs it’s good to check these values, maybe like this:

``````def cheap(weight):
print(cost_of_ground_shipping(weight))
print(cost_of_drone(weight))
print(cost_of_prem)
if cost_of_ground_shipping(weight) < cost_of_drone(weight):
return "ground is cheaper"
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) or (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"
print(cheap(41.5))
``````

because it can reveal whether your issue is in the functions that calculate cost, or in the function that calculates the cheapest.

1 Like

So it’s clearly something with my third if statement that is still broken.

``````def cheap(weight):
print(cost_of_ground_shipping(weight))
print(cost_of_drone(weight))
print(cost_of_prem)
if cost_of_ground_shipping(weight) < cost_of_drone(weight):
return "ground is cheaper"
if cost_of_ground_shipping(weight) > cost_of_drone(weight):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) and (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"

print(cheap(1000))
``````

That helps a bunch in terms of comparison. But in my head it makes sense.

if cost_of_ground_shipping(weight) IS GREATER THAN cost_of_prem AND cost_of_drone(weight) IS GREATER THAN cost_of_prem

then return “premier rate is cheaper” because the other two values are higher.

1 Like

But also make sure you can get to the third statement. Just because ground shipping is cheaper than drone, should that stop the function? What if premier is cheaper than ground?

Remember that return statements exit your function right away once they run, so if there’s a return in the conditional, that means all other permutations must be ruled out.

@jrb0831 you’re very close

1 Like

Okay so it sounds like I’m close but every number I put into (weight) it’s saying that the “ground is cheaper” even when it clearly isn’t.

something else might be up

1 Like

After a long night of thinking and with your help I think I got it.

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

#print(cost_of_ground_shipping(41.5))

cost_of_prem = 125.00

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

#print(cost_of_drone(41.5))

def cheap(weight):
print(cost_of_ground_shipping(weight))
print(cost_of_drone(weight))
print(cost_of_prem)
if (cost_of_ground_shipping(weight) < cost_of_drone(weight)) and (cost_of_ground_shipping(weight) < cost_of_prem):
return "ground is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_drone(weight)) and (cost_of_drone(weight) < cost_of_prem):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) and (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"

print(cheap(25))

``````
1 Like
``````if (cost_of_ground_shipping(weight) < cost_of_drone(weight)) and (cost_of_ground_shipping(weight) < cost_of_prem):
return "ground is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_drone(weight)) and (cost_of_drone(weight) < cost_of_prem):
return "drone is cheaper"
if (cost_of_ground_shipping(weight) > cost_of_prem) and (cost_of_drone(weight) > cost_of_prem):
return "premier rate is cheaper"
``````

That’ll do it! I suspect you can also write else for the last bit (instead of the entire conditional) if it leaves out all other possibilities (you could simplify it with algebra to verify).

1 Like

What do you mean by the entire conditional? You mean replace the last if statement with an else?

1 Like

Yea

``````def find_smallest(a,b,c):
if (a < b and a < c):
return a
if (b < a and b < c):
return b
else:
return c
``````