Sal's shippping

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

premium = 125.00

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

def shipping(weight):
g = ground(weight)
d = drone(weight)
p = premium
if g < d and g < p:
ship = ‘Ground’
return g
return price and ship
elif d < g and d < p:
ship = ‘Drone’
return price and ship
else:
ship = ‘Premium’
price = d
print(‘The cheapest method is $%.2f shipping and costs %s.’) %(ship, price)

print (shipping(4.8))
print (shipping(41.5))

______________________________________________________________–
Traceback (most recent call last):
File “script.py”, line 47, in
print(‘The cheapest method is $%.2f shipping and costs %s.’) %(ship, price)
NameError: name ‘ship’ is not defined

This is doing my head in - 'price is defined throughtout the program and as a last ditch try I definded again ‘price = 0’ which I don’t thik should be there but I want it to run. However it still hasn’t worked. As far as I understand it defining a variable is done when you put = after the variable, so what is the problem. Sorry really frustrated and as much as errors are part of the learning curve thisis not teaching me anything apart from how to get cross. Please help, I know it is going to be really stupid but I wouldn’t forget it. Thank you

2 Likes

ship is a string, not a float.

That will be a boolean. Is that what you wish to return?

2 Likes

Hello mtf,
Thanks for coming back to me. Yes sorry about the ‘ship’ part of my desparate fiddling to get rid of the ‘print not defined’, I had been swaping round each bit - $%.2f and price - and I obviously forgot to put one half back in its proper place.
Also regarding ‘return price and ship’ no I don’t want a boolean but would be interested to know how that would return a boolean in as simple language as possible, please.
However even though I have corrected ‘ship’ and removed ‘return price and ship’ I am still getting the ‘not definded’ error message only now it is ‘price’ (line 47). It seems to be that field or it is not about variable definition at all but that is the closest error it can come up with. Please help me understand as that is the only way I will learn. Thanks

1 Like

Hi again I know I have not waited for your response but I seem to have ‘fiddled’ the problem away (as in I can’t quite work out how I did it)
So thank you for your help and hope you have a good day.

3 Likes

AND is a logical operation that returns True if both operands are truthy, else False.

2 Likes