-1

How can I store values in lists and later compare against each other

all_click = []                                            
all_click_red = []



while True:

    a = input('give a number: ')                          
    b = input('give a second number: ')

    if a == 1:
        all_click.append(a)                               

    if b == 1:                                            
        all_click_red.append(b)                           

    if all_click_red[-1] and all_click[-1] == 1:          
        print('all good')                                 

    else:                                                 
        print('false')     

Because I get an error like this:

 if all_click_red[-1] and all_click[-1] == 1:
     IndexError: list index out of range

2 Answers2

0

The reason for that is that one of the lists may not have a value in it.

You'll probably want something like this:

if len(all_click) > 0 and len(all_click_red) > 0 and all_click_red[-1] == 1 and all_click[-1] == 1:          
    print('all good')                                 

else:                                                 
    print('false')     

Never hurts to check for index safety when accessing indexes in a list like this.

I also added an equality on the all_click_red number check. I'm assuming that's what you were after. Apologies if I misjudged

Craig Brett
  • 2,295
  • 1
  • 22
  • 27
0

You are append something to the list only if it's value is equal 1. If it's not equal 1, the list is empty, and list[-1] will be out of range, raising this error.