What is wrong with this code?


def G_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
    price_per_pound = 4.75
  return 20 + (price_per_pound * weight)

def D_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
    price_per_pound = 14.25
  return price_per_pound * weight


cost_of_P_G_shipping = 125.00

def best_shipping_value(weight):
  ground = G_shipping(weight)
  drone = D_shipping(weight)
  p_grund = cost_of_P_G_shipping
  if ground < drone and ground < p_ground:
    cost = ground
    method = "ground"
  elif drone < ground and drone < p_ground:
    cost = drone
    method = "drone"
    cost = p_ground
    method = "p_ground"

print(f"The best shipping option is {method}, will cost you only {Cost}.")


Please check the following link and how to correctly format code submitted to the forums. It makes reading and subsequently finding any errors much easier-

Is that the exact code you used? I can see typos/errors in the print statement: there’s no variable Cost. There’s also a p_grund in a function which I guess is a typo.


I’ve fixed the code formatting.

@kasem007, unlike with other languages which use curly braces { } to demarcate code blocks, Python relies entirely on indentation. The forum does not, by default, display your code with the whitespace at the start of statements and this makes it harder for us to debug your program. If you can format your code properly - like I’ve done in your post, this time - it’ll be a big help in future.

1 Like

For a weight of:

weight = 10 / 3

your code will report a minimum of 125
the minimum cost for that weight, according to your cost functions, is 30

so your logic for finding the smallest value is wrong