Hi! Please explain why after 3 every following numbers are true?

found=False
print('Before', found)
for value in(9,41,12,3,74,15):
    if value == 3:
        found=True
    print(found, value)
print('After', found)

Because when value == 3 is True you change the value of the global variable found to True, so after that the global variable found will always have a value of True

Thank you. Seems this is a general rule that must simply be followed

If you write break right after “found=True” you might get the behavior you want: stop search right after a 3 is found. However this way it will skip print(found, value).

Great tip. Thanks a lot

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.