+ 1
Can someone check and help fix my python code?
I'm trying to make a while loop with continue and break. I can't figure it out. Throw some tips in the answers and maybe the correction:) https://code.sololearn.com/cE9nH5IvpEo5/?ref=app
1 Answer
+ 5
i = 0
while True:
i = i + 1
if i == 3:
print("Skipping 3")
continue
At line 6, you inadvertently created an infinite loop. What continue does is go back to the top of the loop. So what you're telling python is every-time it reaches line 6, go back to the top. It'll never reach the break statement located at the bottom. You can fix this by simply indenting line 6 under the if statement, and line 10 so it doesn't exit the loop after 1 run:
i = 0
while True:
i = i + 1
if i == 3:
print("Skipping 3")
continue
if i == 7:
print("Done")
print(i)
break
You could also just tell the while loop to stop at 7:
i = 0
while i != 7:
i = i + 1
if i == 3:
print("Skipping 3")
continue
print("Done")
print(i)