You can use the input() function to restart a while loop in Python. And use the if statement to restart loop count.
Here is the simple syntax for it, use your own logic.
i=2 while i < n: if something: do something i += 1 else: do something else i = 2 #restart the loop
Example restart a while loop in Python
Simple example code while loop restart if user enter “0”, otherwise loop will run until “i < 5”. To stop it use the break statement.
i = 0 while i < 5: restart = (input("Enter 0 to restart loop: ")) if restart != "0": print("Loop ", i) i += 1 else: print("Loop Restarted") i = 0 # restart the loop
Do comment if you have any doubts or suggestions on this Python while loop code.
Note: IDE: PyCharm 2021.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.