How to Start Again Python Middle of Loop
How to Loop Back to the Beginning of a Plan in Python?
Here, we will see how to loop back to the starting time of the program in Python. In other words, the program'south control is at some point other than the showtime, and we want the program to start from the tiptop once again. Consider the effigy beneath to understand this concept.
Loop back in Python
In this post, we volition talk about ii approaches.
1. Using a Loop
We tin loop dorsum to the showtime by using a control flow statement, i.east., a while statement. To do that, wrap the complete program in a while loop that is always Truthful.
Moreover, add together a continue statement at a bespeak where you want to start the program from the beginning. You besides demand to add some lawmaking such every bit a suspension statement to stop your program.
Otherwise, the program will run infinitely, and we never desire that.
How to loop back in Python 2
Suppose nosotros have a program that takes the distance and fourth dimension from the user and calculates the speed.
distance = bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the time in hours: ")) speed = distance/time impress("Speed is:", speed,"kph") Now, nosotros want to start from the commencement if the user wants to perform another calculation. To do that, nosotros add together a while statement at the top.
We likewise use a go along statement to restart if the user enters aye. If the user wants to quit, the continue statement will not run, and the program will terminate. Consider the code below that implements this.
while True: distance = float(input("Enter the altitude in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = distance/time print("Speed is:", speed,"kph") check = input("Do you desire to quit or start over again? enter Y to restart or another primal to end: ") if check.upper() == "Y": #go back to the top continue print("Adieu...") intermission #go out
Looping back in Python Output
2. Using a Function
We can also loop back to the start past using a function. Instead of wrapping the whole code in a while loop, nosotros create a function and put our program at that place. If the user wants to continue, we will telephone call the procedure again. Otherwise, nosotros will exit the plan.
Consider the same instance implemented using a function.
def repeat():
distance = float(input("Enter the distance in kilometers: "))
time = float(input("Enter the time in hours: "))
speed = altitude/time
print("Speed is:", speed,"kph")
check = input("Do you want to quit or start proceeds, enter Y to restart or another to end ?: ")
if check.upper() == "Y": #loop back to the outset
echo()
print("Goodbye...")
leave() #exit the program
repeat() Output
Looping back in Python outcome of function approach
Read about ways to loop back to the beginning of a program in Python.
Hey guys! It's me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with y'all guys how I stay on top of the game! I run several highly assisting blogs & websites and beloved to speak about these project whenever I get a chance to do and so. I do this full-fourth dimension and wholeheartedly. In fact, the moment I stopped working an viii-to-five chore and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can go on this path also! Don't allow anyone tell you that this can't be done. Sky's the limit, actually…as long as you BELIEVE in it! And it all starts right here..at Maschituts!
Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/
0 Response to "How to Start Again Python Middle of Loop"
Post a Comment