Sal's Shipping

So,

I have done this two ways.

I began writing my own code running into issues along the way which I expect. But they began to become hindering so I began to waych the video hence why it looks majorly JUST LIKE THE VIDEO.

It taught me alot I think but I still run into issues… like below.



def ground_shipping(weight):

  if weight <= 2:
    price_per_pound = 1.50
  elif weight <= 6:
    price_per_pound = 3.00
  elif weight <= 10:
    price_per_pound = 4.00
  else:
    price_per_pound = 4.75
  cost = 20 + (price_per_pound * weight)  
  return cost

def drone_shipping(weight):

  if weight <= 2:
    price_per_pound = 4.50
  elif weight <= 6:
    price_per_pound = 9.00
  elif weight <= 10:
    price_per_pound = 12.00
  else:
    price_per_pound = 14.25
  cost = price_per_pound * weight  
  return cost

premium_ground_shipping = 125.00

def cheapest_shipping(weight):
  ground = ground_shipping(weight)
  drone = drone_shipping(weight)
  premium = premium_ground_shipping
  
  if ground < premium and ground < drone:
    method = "Standard Ground"
    cost = ground
  elif premium < drone and premium < ground:
    method = "Premium Ground"
    cost = premium
  else:
    method = "Drone"
    cost = drone
    
    print("The cheapest option available is $%2f and via %s."
    %(cost, method))
    

print(ground_shipping(8.4))
print(drone_shipping(1.5))
print(premium_ground_shipping)
print(cheapest_shipping(1))
print(cheapest_shipping(100))


At 1 and 2 it tells me

The cheapest option available is $4.500000 and via Drone.
None - This is for a value of 100 and 41.5.

for some reason it automatically defaults to drone.

I also did it like this…

def cheapest_shipping(weight):
  ground = ground_shipping(weight)
  drone = drone_shipping(weight)
  premium = premium_ground_shipping
  
  if ground < premium and drone < premium:
    return "You should ship with Ground Shipping, which will cost you $" + str(ground)
  elif drone < premium and ground < premium:
    return "You should ship with Drone Shipping, which will cost you $" + str(drone)
  else:
    "You should ship with Premium Ground Shipping, which will cost you $" + str(premium)
    

print(ground_shipping(8.4))
print(drone_shipping(1.5))
print(premium_ground_shipping)
print(cheapest_shipping(4))
print(cheapest_shipping(41.5))


You should ship with Ground Shipping, which will cost you $32.0
None

Though even here the price isn’t correct as @ 4$

ground = 4 * 3 = 12 + 20 = 32
drone = 9 * 4 = 32

which would be right.

Please assist

1 Like

Can the second even be done that way?

1 Like

You tell us. Did it work as expected?

1 Like

No not at all, I got one response that would not express the expected response and am trying to find out what I did wrong on the first

1 Like

How conclusive is comparing to premium?

1 Like

Its circular, gotcha… at least that’s my take as I am comparing both to the premium just in reverse. I’m thinking from a left to right context but better the first was the video and now I am going to go back and look. I moved on and saved progress so I am in lists now but in the same I think I see what you are stating. It was a long day of programming so yea lol.

1 Like

Yea so the first was the right and second was the mistake though I may rewrite the second to see the difference. But even on the first above 2 dollars it messes up.

1 Like

You have the right idea caching the individual results. Premium is a last resort so your code should not assign it any importance in the logic. That means no comparisons.

If drone is less than ground then is drone less than premium? That’s how this one gets figured out. The same is true if ground is less than drone.

1 Like

I’ll try that I appreciate it

1 Like

I see whats happening now, its going directly to the else statement so I need to check my controlflow to see why it bypasses the other two

1 Like

Two of the values are variable, and one is fixed. That is why I suggested not comparing to the fixed value until after choosing from between the other two. There is still a comparison to be made. Belay the statement above about no comparisons.

2 Likes

That’s funny as that is how the experienced programmer in the video does it. I see what you are saying though fixed or not there shouldn’t be an issue right? It’s still a fixed value… but I’ll give that a try as well.

1 Like

There is no issue in any code that works. The second example has issues, then so too does the first.

Consider, we have three values, a, b, c. If one is greater than one of the other two, then it (the lesser one) can be discarded. That leaves two values for comparison.

if a > b
  then if a > c
    else c is the greatest
else if b > c
else c is the greatest

Now write that in terms of least, instead of greatest.

3 Likes

I actually did what you said, moved on and came back to it. Thank you for your input.

1 Like