Python Control Flow

Hi I am getting the following error message:
Traceback (most recent call last):
File “script.py”, line 52, in
% (cost, method)
NameError: name ‘cost’ is not defined

Can someone help what is wrong with the code?

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
  elif weight > 10:
    price_per_pound = 4.75
    
    return 20 + (weight*price_per_pound)
    
    
print(ground_shipping(8.4))  

shipping_cost_premium = 125.00


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
  elif weight >10:
    price_per_pound = 14.2
    
  return weight*price_per_pound
    
print(drone_shipping(1.5))
      
def cheapest_shipping_method(weight):
  
  ground = ground_shipping(weight)
  premium = shipping_cost_premium
  drone = drone_shipping(weight)
  
  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium
  else:
    method = "drone"
    cost = drone
    
    
print ( 
"The cheapest option available is $%.2f with %s shipping."
    % (cost, method) 
)    
    
cheapest_shipping_method(4.8)    
cheapest_shipping_method(41.5)

the error line:

print ( 
"The cheapest option available is $%.2f with %s shipping."
    % (cost, method) 
)    

cost is not within the global name space, its part of the cheapest_shipping_method function but the print statement is not in the body of this function

Thank you, I put in inside the function but now I’, getting:

Traceback (most recent call last):
File “script.py”, line 55, in
cheapest_shipping_method(4.8)
File “script.py”, line 39, in cheapest_shipping_method
if ground < premium and ground < drone:
TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘float’

Okay, and how could we figure this out? One of the many things you need to learn is how to debug your code.

so one of the variable used in your comparison has a None value, which one? How could we figure this out? Maybe we can print all the values of the variables used in the comparison?

If another error code, apply the same thinking steps as i just did.

I did and especially the last function based on the tutorial video for this specific project.

Aside from the different function names in the video for, I can’t find anything wrong.

if i put a print statement in your function:

def cheapest_shipping_method(weight):
  
  ground = ground_shipping(weight)
  premium = shipping_cost_premium
  drone = drone_shipping(weight)

  print(ground, premium, drone)
  if ground < premium and ground < drone:
    method = "standard ground"
    cost = ground
  elif premium < ground and premium < drone:
    method = "premium ground"
    cost = premium
  else:
    method = "drone"
    cost = drone
    
    
    print ( 
    "The cheapest option available is $%.2f with %s shipping."
        % (cost, method) 
    )    

i can clearly see ground has None value. Where does this None value come from?

Fixed. The problem was with the indentation in the definition of the first function. Thanks.

I hope you also learned something about debugging.

Just for my own personal curiosity, I said the following:

which didn’t seem to result in much, but when i did this step for you, you figured it out. How come?

The ground variable accepted the return value from the output of the first function.
If you see my first post you will notice that the return line has a bad indentation an hence the function can not produce a value for ground which resulted in the non value error. When you said “Where does this None value come from?” I checked the first function and noticed the issue. :slight_smile: