简体   繁体   中英

Python says variable is not defined but it is I believe

So im learning python in school but the teacher sucks so thats why im here. I am doing an assignment but the variable decided it didnt want to work even though it was working yesterday

Error:

Traceback (most recent call last):
  File "food.py", line 44, in <module>
    print(Fore.BLUE + 'You choose ' + sandwich_choice + ' with a price of ' + sandwich_price)
NameError: name 'sandwich_price' is not defined

Code that produces the error:

if sandwich_choice_input == 'Chicken Sandwich' or sandwich_choice_input == 'Beef Sandwich' or sandwich_choice_input == 'Tofu Sandwich':
  sandwich_choice = sandwich_choice_input
  chicken = '$5.25'
  beef = '$6.25'
  tofu = '$5.75'
elif sandwich_choice_input == 'Chicken Sandwich':
  sandwich_price = chicken
elif sandwich_choice_input == 'Beef Sandwich':
  sandwich_price = beef
elif sandwich_choice_input == 'Tofu Sandwich':
  sandwich_price = tofu
  
print(Fore.BLUE + 'You choose ' + sandwich_choice + ' with a price of ' + sandwich_price)

I tried the variable I tried moving the variable to a db but that didnt work either and I wanted it to put the price of the sandwich which is already pre defined im also extremely new to python so please dont judge

I suppose that earlier on in your code you're asking, via input() , for a variable called sandwich_choice_input . The problem with your technique is that it's error prone: It relies on the user typing the answer to this input() exactly as your three answers (Chicken Sandwich etc.) are spelled. Even a slight typo, such as a missing letter or incorrect capitalisation, will mean no value is assigned to the variable sandwich_choice , and your code breaks.

To avoid this problem, you could, for instance, present (with print() ) the three available choices to the user, numbered 1-3, and the user simply types the number of the desired choice; you would then compare the input against '1' etc. rather than against 'Chicken Sandwich' etc. This would considerably reduce the probability of errors.

Also, the line elif sandwich_choice_input == 'Chicken Sandwich': isn't correct; this needs an if , not an elif (because there is no preceding if that the elif could be "else" to).

I suggest you to do that:

  sandwich_choice_input = input("what do you want to eat ?")
  choices = ['Chicken Sandwich', 'Beef Sandwich', 'Tofu Sandwich' ]
  sandwich_choice = ""
  if sandwich_choice_input in choices :
      sandwich_choice = sandwich_choice_input
      chicken = '$5.25'
      beef = '$6.25'
      tofu = '$5.75'
    if sandwich_choice_input == 'Chicken Sandwich':
      sandwich_price = chicken
    elif sandwich_choice_input == 'Beef Sandwich':
      sandwich_price = beef
    elif sandwich_choice_input == 'Tofu Sandwich':
      sandwich_price = tofu
  
    print(Fore.BLUE + 'You choose ' + sandwich_choice + ' with a price of ' + sandwich_price)
  else : print("Sorry, your order is not available")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM