Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For cyclus python3 with if statement

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading