How To Restart A Loop In Python? - Finxter
Maybe your like
Problem Formulation
Given a Python loop and a logical condition. How to restart the loop if the condition is met?
for/while ...: # ... loop body here ... if condition: # restart loopSolution 1: Reset While Loop
The while loop checks a condition in order to determine whether the loop body should be executed or not. You can reset the condition to the initial value in order to effectively restart the loop.
In the following example, you use the loop condition i < 3 and increase the value of i in each loop iteration. You can reset the value of the loop variable to i = 0 to restart the loop as soon as the user types in 'r'.
i = 0 while i < 3: print('i =', i) # loop body # Restart loop logic i = i + 1 if input() == 'r': i = 0 # force restartYou use the Python built-in input() function to take the user input in each iteration and return it as a string.
Here’s one example run where the loop is not restarted because the user never used 'r' as an input (every other line is the user input, whereas the second line is the first user input line):
i = 0 alice i = 1 bob i = 2 carlAnd here’s an example where the loop is restarted in the third iteration due to the 'r' user input:
i = 0 alice i = 1 bob i = 2 r i = 0 alice i = 1 bob i = 2 carlIn case you need a quick refresher on how to process user inputs, check out this tutorial:
Solution 2: Nested Restart Loop
An alternative to force a loop to restart if a certain condition is met is to embed the restart loop into an outer while loop to repeatedly execute it until an exit condition is met.
The following code shows how to do this in Python:
restart = True while restart: for i in range(3): print('i =', i) # loop body # Default: execute once restart = False # Restart loop logic (any restart condition): if input() == 'r': restart = True break # force restartWe create a variable restart that is initially set to True. The outer loop will execute until the restart variable is set to False.
Now, you run the main loop that must be restarted potentially if a certain condition is met. The code uses the restart condition input() == 'r', so if the user types in the character 'r', the loop is restarted.
The restart is accomplished by setting the restart variable to True again, so the outer loop will run once again which means the whole inner for loop is restarted.
This whole cycle is repeated until the restart condition is never met. This automatically sets the restart variable to False which means the outer loop is not executed again, which means that the inner loop is not restarted.
Here’s one example run where the loop is not restarted because the user never used 'r' as an input:
i = 0 alice i = 1 bob i = 2 carlAnd here’s an example where the loop is restarted in the third iteration due to the 'r' user input:
i = 0 alice i = 1 bob i = 2 r i = 0 alice i = 1 bob i = 2 carlWhere to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
Tag » How To Restart Loop C++
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
How To Restart Loop Again? - C++ Forum
-
How Can I Restart A Loop: C++ - For Beginners
-
C++ Loop Help To Restart To Beginning Of Program - C Board
-
Restarting A While Loop Without Finishing The Remaining Code.
-
How To Use A While Loop To Restart A Game - Quora
-
How To Restart Void Loop If Condition Is Met - Arduino Stack Exchange
-
C++ How To Restart The Loop If User Just Presses Enter Or If The Input Is ...
-
JavaScript: Continue Statement - TechOnTheNet
-
How Do I Restart A Function From Within Itself? - Codecademy
-
How To Restart A For Loop In Go - Freshman.tech
-
Exit(0); Restart Loop - Programming Questions - Arduino Forum
-
How Can I Restart While Loop After Certain Condition (if) Satisfied In C++?
-
Going To Start Of A Loop - Scripting Support - DevForum | Roblox