A bug with Python Chatbot shipping project

Hello,

I’m running the shipping project with Chatbot class. The code I wrote is as follows. However, there is a bug with the undefined variable “Cost”. I don’t know how that happened. Can someone run the code and let me know the problem? Thanks very much!

def ground_shipping(weight):
if weight <=2.0:
return weight1.5 +20
elif weight <=6.0:
return weight
3+20
elif weight <=10:
return weight4+20
else:
return weight
4.75+20
print(ground_shipping(8.4))
preminumground_shipping = 125

def drone_shipping(weight):
if weight <=2.0:
return weight4.5
elif weight <=6.0:
return weight
9
elif weight <=10:
return weight12
else:
return weight
14.25
print(drone_shipping(1.5))

def customer_favor(weight):

ground = ground_shipping(weight)
preminum = preminumground_shipping
drone = drone_shipping(weight)

if ground < preminum and ground < drone:
cost = ground
method = “Standard Shipping”
elif (preminum < ground) and (preminum< drone):
cost = preminum
method = “Premium Shipping”
else:
cost = drone
method = “Drone Shipping”
print(
“The cheapest way of shipping will cost $%.2f with %s shipping.”
% (cost, method)
)
customer_favor(4.8)
customer_favor(41.5)

Hi!

Welcome to the forums :slight_smile:

Could you try pressing the format button Screen Shot 2020-12-15 at 3.43.13 PM and then pasting the code in-between the tick marks? Screen Shot 2020-12-15 at 3.42.45 PM With python, errors are very syntax dependent.

2 Likes

Here you go!

def ground_shipping(weight):
  if weight <=2.0:
    return weight*1.5 +20
  elif weight <=6.0:
    return weight*3+20
  elif weight <=10:
    return weight*4+20
  else:
    return weight*4.75+20
print(ground_shipping(8.4))
preminumground_shipping = 125

def drone_shipping(weight):
  if weight <=2.0:
    return weight*4.5
  elif weight <=6.0:
    return weight*9
  elif weight <=10:
    return weight*12
  else:
    return weight*14.25
print(drone_shipping(1.5))


def customer_favor(weight):

  ground = ground_shipping(weight)
  preminum = preminumground_shipping
  drone = drone_shipping(weight)

  if ground < preminum and ground < drone:
    cost = ground
    method = "Standard Shipping"
  elif (preminum < ground) and (preminum< drone):
    cost = preminum
    method = "Premium Shipping"
  else:
    cost = drone
    method = "Drone Shipping"
print(
  "The cheapest way of shipping will cost $%.2f with %s shipping."
  % (cost, method)
  )
customer_favor(4.8)
customer_favor(41.5)type or paste code here

It’s your indentation for the print in customer_favor(). The variable cost only lives within the scope of customer_favor() and when it’s not indented the compiler thinks it’s not part of the function.

def customer_favor(weight):

  ground = ground_shipping(weight)
  preminum = preminumground_shipping
  drone = drone_shipping(weight)

  if ground < preminum and ground < drone:
    cost = ground
    method = "Standard Shipping"
  elif (preminum < ground) and (preminum< drone):
    cost = preminum
    method = "Premium Shipping"
  else:
    cost = drone
    method = "Drone Shipping"
  print("The cheapest way of shipping will cost $%.2f with %s shipping."  % (cost, method))
  #note the indentation
  #here cost exists
#here cost doesn't exist

It makes sense! The bug was fixed now. Thank you a lot!