print(f"n before loop is {n}")
while True:
for i in range(n):
if n == 20:
break
else:
print(n)
n = n + 1
Enter n 10
n before loop is 10
10
11
12
13
14
15
16
17
18
19
^CTraceback (most recent call last):
File "/home/ubuntu/1_5.py", line 5, in <module>
for i in range(n):
^^^^^^^^
KeyboardInterrupt```
As you can see, i had to ctrl+c every time to exit program. It works properly as intended in other programs and exit as it should be. But here in this program it doesn't exit the program but only exits the loop. Thanks for reading my question.
You're only breaking out of the for loop currently. You could set a run flag to break out of both loops like this:
n = int(input("Enter n "))
print(f"n before loop is {n}")
run = True
while run:
for i in range(n):
if n == 20:
run = False
break
else:
print(n)
n = n + 1`