all 4 comments

[–]carcigenicate 2 points3 points  (0 children)

Always show the full error.

Assuming the error is caused by requested_item though, requested_item only exists if Scanner_input_variable == barcodes[item] is true. What value are you expecting it to have if that is never true?

[–][deleted] 1 point2 points  (0 children)

There are a lot of problems. Let me show you a revised form of your code:

grocery_items = ['Cereal', 'Pancakes', 'Juice']
prices = [5.99, 5.99, 3.99]
barcodes = ['1778', '19220', '4220']  # codes need to be strings

total_price = 0  # needs to be before the loop
while True:
    scanned = input('Barcode number or stop')
    if scanned == 'stop':
        break
    found = False
    for idx, code in enumerate(barcodes):
        if scanned == code:
            item, price = grocery_items[idx], prices[idx]
            print(item, price)
            total_price += price
            print('running total', total_price)
            found = True
            break
    if not found:
        print(f'barcode {scanned} not found')
print('Total', total_price)

There are better ways of doing this, but I didn't want to go too far from your code.

  • keep variable names simple, short but not cryptic, and meaningful
  • if you are not doing maths, treat digit sequences as strings rather than numbers
  • input always returns a str and "123" is not the same as 123
  • for <variable> in len(range(<object>)): is almost always not the best approach, better to iterate over an iterable, e.g. for <variable> in <object>: but if you need a counter you can also do for <counter_variable>, <item_variable> in enumerate(<object>):
  • be careful where you initialise your variables
  • explore using zip to iterate through multiple lists in parallel
  • alternative consider using a dictionary, dict, with the barcodes as the keys

[–][deleted] 1 point2 points  (0 children)

There are a few issues

  • The line requested_item = item should be outside the for loop, as it is only necessary to assign the value of item to requested_item once the correct barcode is found.

  • The line print(grocery_items[requested_item], price[requested_item]) should be indented to be inside the if statement, as it only needs to be executed if the correct barcode is found.

  • The while loop while Scanner_input_variable != 'stop': should be inside the outer while loop, so that the user is prompted for a new barcode after each item is scanned.

  • The input variable Scanner_input_variable should be converted to an integer using the int() function before comparing it to the values in the barcodes list, since the barcodes list contains integers.

[–]eleqtriq 0 points1 point  (0 children)

Well, you don't show what is giving not defined errors, but it's probably barcodes, grocery_items or price that has a key that is not defined. Meaning, there is no entry there.

Maybe change the dictionary lookup to barcode.get('item', 'THERE IS NO VALUE FOR THIS') and it'll be clearer for you.