Python Forum
NameError: name 'print_string' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'print_string' is not defined
#1
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!
Reply
#2
"==" 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")
Reply
#3
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation NameError: name 'score' is not defined - Help? MrKnd94 13 4,614 Mar-06-2023, 10:54 PM
Last Post: deanhystad
  How to correct the NameError: name 'xx' is not defined? vokoyo 5 11,333 Feb-17-2021, 05:55 AM
Last Post: delonbest
  NameError: name 'os' is not defined, & load_files(sys.argv[1]) AryaIC 3 4,726 Nov-07-2020, 07:45 PM
Last Post: jefsummers
  Error in code NameError: name ' ' is not defined ppman00 11 6,282 Sep-18-2020, 05:22 AM
Last Post: ndc85430
  "NameError: name 'catName1' is not defined tofif 3 5,672 Jun-24-2019, 06:05 AM
Last Post: perfringo
  NameError x not defined Bruizeh 5 5,300 Feb-27-2019, 10:59 AM
Last Post: Larz60+
  NameError: name 'mailbox_list' is not defined pythonnewb 2 4,769 Aug-06-2017, 09:31 PM
Last Post: pythonnewb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020