Hello all. For Sal’s Shipping, I am sure I have missed something simple, but I cannot decipher why this will not work. I watched the walk through and I get how that solution works and it is more efficient than how I did it, but I would like to know what I did wrong. Thanks!
https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping
Code then output posted below:
# ground based on weight
def ground(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
# premium
premium = 125.00
# drone based on weight
def 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
# what is cheapest
def best(weight):
ground = ground(weight)
drone = drone(weight)
prem = premium
if ground < drone and ground < prem:
print("The cheapest way to ship a " + str(weight) + "pound package is ground and it will be $" + ground + ".")
elif drone < ground and drone < prem:
print("The cheapest way to ship a " + str(weight) + "pound package is by drone and it will be $" + drone + ".")
else:
print("The cheapest way to ship a " + str(weight) + "pound package is with premium ground and it will be $125.00.")
best(4.8)
Traceback (most recent call last):
File "script.py", line 41, in <module>
best(4.8)
File "script.py", line 29, in best
ground = ground(weight)
UnboundLocalError: local variable 'ground' referenced before assignment