0
list = ['xy', 'yz', '=', '1', 'in', '>', 't']

for value in list:
   if value is '0' or '1':
      Zustand = value
      print("Zustand:", Zustand)

The if loop should check if the value of list is 0 or 1. Why does it print every value? I also tried the "==" operator.

Markus
  • 51
  • 6

1 Answers1

1
if value is '0' or '1':

this is evaluated as

if (value is '0') or ('1'):

which is always true. try

if value is '0' or value is '1':
iamtrappedman
  • 176
  • 1
  • 7
  • 5
    The OP should also not be using `is` here; it's implementation-dependent whether `value` refers to the same object that the literal `'0'` refers to. – chepner Sep 16 '22 at 12:41
  • 1
    yes, this is not how `is` be used. I just used as OP seems to use it. this is a clear case to use `==`. `value in ['0', '1']` can be used here. – iamtrappedman Sep 16 '22 at 12:45