Python Forum

Full Version: NameError: name 'print_string' is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def ground_shipping_cost(weight):
  if (weight <= 2): # 2 pounds or less
    price = 1.50
  elif (weight > 2) or (weight <= 6):
    price = 3.00
  elif (weight > 6) or (weight <= 10):
    price = 4.00
  elif (weight > 10):
      price = 4.75

  cost = ((weight * price) + 20)

  return cost

def drone_shipping_cost(weight):

  if (weight < 2):
    price = 4.50
  elif (weight > 2) or (weight <= 6):
    price = 9.00
  elif (weight > 6) or (weight <= 10):
    price = 12
  elif (weight > 10):
    price = 14.25

  return price

premium_ground_shipping = 125.00

def cheapest_shipping (weight):
  if drone_shipping_cost(weight) < ground_shipping_cost (weight):  
    ret = drone_shipping_cost(weight)
    ship_method = 1 # "ground shipping"
  elif ground_shipping_cost (weight) < drone_shipping_cost(weight):
    ret = ground_shipping_cost (weight)
    ship_method = 2 # "drone shipping"
  else:
    ret = premium_ground_shipping
    ship_method = 3 # "premium shipping

    return ret

  if (ship_method == 1):
    print_string == "ground_shipping"
  elif (ship_method == 2):
    print_string == "drone shipping"
  elif(ship_method == 3):
    print_string == "premium shipping"

  ship_price = cheapest_shipping (4.8) # input weight to calculate

t_string = "You should ship using: " + str(print_string) + " it will cost " + str(ship_price)

print(t_string)
please can someone fix this line
t_string = "You should ship using: " + str(print_string) + " it will cost " + str(ship_price)
I'm a noob coming from C and would like this script to work. I see that the str() function is working for the str(ship_price) successfully because I have used it for the cheapest_shipping (4.8) function, how do I get around doing that. Thanks!
"==" is the equality comparison, "=" is assignment.

print_string == "ground_shipping" compares the two and evaluates to True or False, it does not set the value of print_string.
print_string = "ground shipping"
if print_string == "ground shipping":
    print("Using ground shipping")
check line 41, do you really want the return statement at that place in the function and with that indentation? what will happen if the else block is executed? print_string has local scope - it exists only inside the function and is not accessible outside it. Do you want lines 43-48 inside the function. If it should be outside - ship_method (local scope inside the function) will not be accessible and will cause NameError
Note that there is a LOT of redundant code...