# Help with Shipping Method

#1

Here is the code I wrote for figuring out the cheapest shipping method. However, no matter the weight it always prints out the ground shipping method. Can anyone tell me what I did wrong? Thanks!

def cost_of_ground_shipping (weight):
if weight <= 2:
return “\$” + str(weight * 1.5)
if weight > 2 and (weight <= 6):
return “\$” + str(weight * 3)
if weight > 6 and weight <= 10:
return “\$” + str(weight * 4)
if weight > 10:
return “\$” + str(weight * 4.75)
#print(cost_of_ground_shipping(8.4))
price = 125

def cost_of_drone_shipping (weight):
if weight <= 2:
return “\$” + str(weight * 4.5)
if weight > 2 and (weight <= 9):
return “\$” + str(weight * 12)
if weight > 6 and weight <= 12:
return “\$” + str(weight * 14.25)
if weight > 10:
return “\$” + str(weight * 4.75)
#print(cost_of_drone_shipping(1.5))
def compare_shipping (weight):
if cost_of_drone_shipping(weight) or premium_ground_shipping > cost_of_ground_shipping(weight):
return cost_of_ground_shipping(weight)
if premium_ground_shipping(weight) < cost_of_drone_shipping(weight) or cost_of_drone_shipping(weight):
if cost_of_ground_shipping(weight) or premium_ground_shipping (weight) > cost_of_drone_shipping(weight):
return cost_of_drone_shipping(weight)

def compare_shipping_type (weight):
if compare_shipping(weight) == cost_of_ground_shipping(weight):
return “by Ground Shipping”
if compare_shipping(weight) == cost_of_drone_shipping(weight):
return “by Drone Shipping”

def which_shipping_method (weight):
print(compare_shipping(weight))
print(compare_shipping_type(weight))

print(which_shipping_method(41.5))

#2

You don’t need to define a function for this, just use a variable.
variable_name = 125

The problem lies in this function. As you know ‘==’ means ‘is equal to’.
So, if all the if statements ever do is compare whether the price is equal to the same price, it can either conclude True or False. If True it will run this piece of code:

In your case the code below will always return True, since both values will always be the same.

print( “compare shipping”, compare_shipping(weight))
print( “cost of ground”, cost_of_ground_shipping(weight))

in front of:

if compare_shipping(weight) == cost_of_ground_shipping(weight):

And you will notice it will print out twice the same value:

compare shipping \$197.125
cost of ground \$197.125

All you need to do is change the logic of the compare function.
To something similar like this:

Then you need to only call this function.
Hope this helps, if not feel free to ask clarifications.

Cheers!

#3

This has been very helpful! Appreciate it

#4

Hi! First ever post. Excuse (and correct!) me if I make a mistake in posting.

I dont understand why my code is wrong I dont know why it posts “None”)

def cost_ground_shipping(weight):
if weight<=2:
return 1.5*(weight) + 20
elif weight<=6:
return 3*(weight) + 20
elif weight<=10:
return 4*(weight) + 20
else:
return 4.75*(weight) + 20

#print(cost_ground_shipping(8.4))

def cost_drone_shipping(weight):
if weight<=2:
return 4.5*(weight) + 0
elif weight<=6:
return 9*(weight) + 0
elif weight<=10:
return 12*(weight) + 0
else:
return 14.25*(weight) + 0

def cheapest(weight):
method3 = 125
method2 = cost_drone_shipping(weight)
method1 = cost_ground_shipping(weight)

if method1<method2 and method1<method3:
print(“Ground Shipping is cheapest at \$”+str(method1))
elif method2<method3 and method2<method1:
print(“Drone Shipping is cheapest at \$”+str(method2))
elif method3<method1 and method3<method2:
print(“Premium Shipping is cheapest at \$”+str(method3))

print(cheapest(1.8))
print(cheapest(4.8))
print(cheapest(41.5))

#5

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