Home > OS >  For cyclus python3 with if statement
For cyclus python3 with if statement

Time:04-30

What is wrong with this cycle, please that the output is

0
One
1
One
2
One
3
One
4
One
5
One


for i in range(6):
    print(i)
    if i == 0 or 2 or 4:
        print('One')
    else:
        print('Two')

I would expect alternate printing of One and Two. Many thanks.

CodePudding user response:

Make the below changes in your if statement:

for i in range(6):
    print(i)
    if i == 0 or i == 2 or i == 4:
        print('One')
    else:
        print('Two')

Your if i == 0 or 2 or 4: is equivalent to if (i == 0) or 2 or 4: and hence will compute as always true.

  • Related