lundi 7 août 2017

Python if/or statement confusion [duplicate]

This question already has an answer here:

I'm very new to coding of any kind and there's something that I'm struggling to understand. I'll break it all down below...

The problem: Find all integers between 1 and 1000 that contain the digits 6 or 7 in them.

My first attempt:

for i in range(1000):
if ('6' or '7') in str(i):
    print(i)

My final solution:

for i in range(1000): if '6' in str(i) or '7' in str(i): print(i)

The final solution works fine but I don't understand why my original code doesn't work. When using the original, only the first digit (6) would be printed. Moreover, if I leave the parentheses out of the original then range(1000) is printed in its entirety which has confused me further. Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire