Boolean comprehension problem

Hello everyone!

I wrote the following code:

i = 0
active = i==0
while active == True:
i+=1
print(active)

(the two last lines of code are of course indented so they follow the while loop).

The problem it prints always true , true , true … , but the i value is being changed so active is false after the first increment , can you explain this please ? Thank you.

1 Like

The value of i is changing, but the value of active is not. You set
active once, before the loop:

active = i==0

but then you never change it again.

Try this instead:

i = 0
active = (i==0)
while active:
    print(i, active)
    i += 1
    active = (i==0)
2 Likes

Ah I understand thank you very much .

1 Like