Sal's shipping! Confusion about output - None

So I don’t understand why, and I’ve looked around, but for some reason, the output of my last function, when specified as 41.5lb, is “None”. I don’t see any reason fo it to do so. Could you help please?


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

def drone_ship(weight):
  if weight < 2:
    cost = 1.50 * 3
    charge = weight * cost
  elif 2 < weight <= 6:
    cost = 3.00 * 3
    charge = weight * cost
  elif 6 < weight <= 10:
    cost = 4.00 * 3
    charge = weight * cost
  else:
    cost = 4.75
    charge = weight * cost
  total_charge = charge
  return total_charge

def lowest_price(weight):
  ground_price = ground_ship(weight)
  drone_price = drone_ship(weight)
  premium = 125
  if ground_price > drone_price:
    if premium > drone_price:
      print("The lowest price is from shipping by drone:")
      return drone_price
    
  elif drone_price > ground_price:
    if premium > ground_price:
      print("The lowest price is from shipping by ground:")
      return ground_price
  elif premium < drone_price and premium < ground_price:
    print("The lowest price is from shipping premium:")
    return premium

print(lowest_price(41.5))




Also please don’t remark about cutting down on use with e.g. the repeated “charge =”
I know, but that isn’t the issue at hand.
Thanks a lot :DD

None is the absence of a return value. And due to the lack of information being printed, its safe to assume none of your conditions evaluate to true

1 Like

So how would I fix that, because I don’t see why the last part isn’t being used? The else at the end of the function, namely:

elif premium < drone_price and premium < ground_price:
    print("The lowest price is from shipping premium:")
    return premium```

maybe start with printing all the variables used in this comparison and see why its not going right? And you are not using else, you use elif (another condition)

1 Like

Because the first if evaluates to true.

The following if does not, so your function returns the default (i.e. unspecified) value of None.

I changed your code:

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

def drone_ship(weight):
  if weight < 2:
    cost = 1.50 * 3
    charge = weight * cost
  elif 2 < weight <= 6:
    cost = 3.00 * 3
    charge = weight * cost
  elif 6 < weight <= 10:
    cost = 4.00 * 3
    charge = weight * cost
  else:
    cost = 4.75
    charge = weight * cost
  total_charge = charge
  return total_charge

def lowest_price(weight):
  ground_price = ground_ship(weight)
  drone_price = drone_ship(weight)
  premium = 125
  if ground_price > drone_price:
    print("Ground price > drone price")
    if premium > drone_price:
      print("Premium > drone price")
      print("The lowest price is from shipping by drone:")
      return drone_price  
  elif drone_price > ground_price:
    print("Drone price > ground price")
    if premium > ground_price:
      print("Premium > ground price")
      print("The lowest price is from shipping by ground:")
      return ground_price
  elif premium < drone_price and premium < ground_price:
    print("premium < drone AND premium < ground")
    print("The lowest price is from shipping premium:")
    return premium

print(lowest_price(41))

Console output is:

Ground price > drone price
None

Because that first if is true, it doesn’t matter what the rest of the elifs do - they’ll never be executed.

1 Like

Thank you guys for your replies! I finally managed to get it working with this, do you see why it now works?

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

def drone_ship(weight):
  if weight < 2:
    cost = 1.50 * 3
    charge = weight * cost
  elif 2 < weight <= 6:
    cost = 3.00 * 3
    charge = weight * cost
  elif 6 < weight <= 10:
    cost = 4.00 * 3
    charge = weight * cost
  else:
    cost = 4.75
    charge = weight * cost
  total_charge = charge
  return total_charge

def lowest_price(weight):
  ground_price = ground_ship(weight)
  drone_price = drone_ship(weight)
  premium = 125
  if ground_price > drone_price and premium > drone_price:
    print("Shipping by drone is cheapest:")
    return drone_price
  elif drone_price > ground_price and premium > ground_price:
    print("Shipping by ground is cheapest:")
    return ground_price
  elif premium < drone_price and premium < ground_price:
    print("Shipping by ground premium is cheapest:")
    return premium

print(lowest_price(41.5))