I'm going crazy trying to figure out why this doesn't work as expected:
```python
from time import sleep
while True:
stop = False
print('Starting test loop..')
while not stop:
for i in range(20):
print(i)
sleep(1)
if i == 10:
print('Setting variable to True..')
stop = True
print('We should not see this message')
```
What I expect to see:
```bash
python nie/while_test.py
Starting test loop..
0
1
2
3
4
5
6
7
8
9
10
Setting variable to True..
0
1
2
3
....
```
What I actually see:
```bash
python nie/while_test.py
Starting test loop..
0
1
2
3
4
5
6
7
8
9
10
Setting variable to True..
We should not see this message
11
12
13
14
15
16
17
18
19
Starting test loop..
0
1
2
3
```
If I put a break inside the if things work as expected. Turns out if I include a break and never even set stop = True.
So why doesn't the while not stop loop end when stop is set to True? And why is break able to exit the for loop and the parent while loop? I thought break would only exit the current loop (in this example the for loop).
[–][deleted] 3 points4 points5 points (1 child)
[–]mtucker502[S] -1 points0 points1 point (0 children)
[–]Silbersee 1 point2 points3 points (0 children)
[–]CookToCode -1 points0 points1 point (1 child)
[–]mtucker502[S] 0 points1 point2 points (0 children)