-3

I compare each element of the list ['a','c'] in an if-statement control flow with a string like this:

charlist = ['a','b']

for el in charlist:
    if el is'a' or 'A':
        print('a is done')
    if el is 'b' or 'B':
        print('b is done')
    if el is 'c' or 'C':
        print('why c?')

Output is:
a is done
b is done
why c?

why does this execute the "if el is 'c' or 'C':" statement?
I think only the first two are executed?

But this solution worked for me

for el in charlist:
    if el == 'a' or el == 'A':
       print('a is done')
    if el == 'b' or el == 'B':
       print('b is done')
    if el == 'c' or el == 'C':
       print('c is done')
Dennis Schaffer
  • 556
  • 7
  • 17

1 Answers1

-1

Try doing it this way for clarity:

charlist = ['a','b']

for el in charlist:
  if el =='a' or el =='A':
     print('a is done')
  if el == 'b' or el =='B':
     print('b is done')
  if el == 'c' or el == 'C':
      print('why c?')
clearshot66
  • 2,292
  • 1
  • 8
  • 17