In this case:
swag = True
i = 0
while swag:
i=i+1
print(swag)
if i == 3:
swag = False
Will the while loop exit after 3 turns?
Does while swag - check if swag exists or if swag is True
while swag: will run while swag is "truthy", which it will be while swag is True, and will not be when you set swag to False.
Does while swag - check if swag exists or if swag is True
It checks if swag is True (or "truthy", I should say). And yes, the loop will exit after 3 iterations because i=i+1 must be executed 3 times until i == 3 and (by the if-statement) swag is set to False, at which point the loop will exit.
But why not check this yourself?
swag = True
i = 0
while swag:
i=i+1
print(swag)
if i == 3:
swag = False
True True True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With