Search code examples
pythondictionarywhile-loopnameerror

Getting an error for 'name not defined' when it has been defined


I'm writing code to have users input items/prices/quantities for a grocery list. The while loop is supposed to continue looping until a variable is no longer true. I defined the variable ahead of the while loop (stop=='true') but it keeps coming back as an error for 'stop' not being defined. I've tried all sorts of combos of random things so the code might look crazy by now. Code below. Thanks!

grocery_list={}
grocery_history={}

stop == 'true'

while stop == 'true' :
    grocery_item['item_name']=input('Item name:\n')
    grocery_item['quantity']=input('Quantity purchase:\n')
    grocery_item['cost']=input('Price per item:\n')
    grocery_history.append(grocery_item)
    if input('Would you like to enter another item?\nType c for continue or q to quit:\n') == 'q':
       stop=='q'

Solution

  • Line 4 should be: stop = 'true'

    One = is for assignment, == is a comparison operator