-1

I have a simple code below that doesn't seem to output the expected result. The code only prints yup once even though I expect the code to print yup three times since that boolean expression will be true three times during the entire for loop iteration. Any advice on how to change my bool expression so that the loop prints yup three times?

a1=['555','666','777',66]
b1=['999','888','333',66]

for a,b in zip(a1,b1):
    if a == ('555'  or  '666' or 66):
        print('yup')
  • 1
    So many duplicates. TLDR: That is the same as a == ‘555’. Hint: a == b or a == c or a == d (or) a in (b, c, d). – user2864740 Jul 31 '20 at 05:19
  • 1
    Yup, or you could also use a in (b, c ,d) – vestronge Jul 31 '20 at 05:20
  • The “why”: https://stackoverflow.com/questions/13870378/python-or-operator-weird-behavior/13870419, the “fix”: https://stackoverflow.com/q/22304500/2864740 (just remove the ‘not’ in this case), and the _inverse_ case: https://stackoverflow.com/q/15112125/2864740 – user2864740 Jul 31 '20 at 05:24
  • @user2864740 sorry im new to stack and didnt do my search thoroughly — should this post be deleted or left as is? My problem has been solved. – BuilderBoba Jul 31 '20 at 05:44
  • It’s your call. I would consider a vote to close it with a duplicate that makes sense to you. – user2864740 Jul 31 '20 at 05:49

1 Answers1

0

maybe this helps highlight your problem

x = ('555'  or  '666' or 66)
print(x)
a1=['555','666','777',66]
for a in a1:
    if a == x:
       print("yup")

the correct check is

either

if a in ('555'  or  '666' or 66)

or

if a == '555' or a == '666' or a == 66   
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179