Python Run Each Loop for Certain Time Before Trying Again
Here, we will see how to loop back to the beginning of the program in Python. In other words, the program'south control is at some point other than the starting time, and we desire the programme to showtime from the superlative again. Consider the figure below to understand this concept.
Loop back in Python
In this post, nosotros will talk most two approaches.
one. Using a Loop
We can loop dorsum to the start past using a command menses argument, i.e., a while argument. To practice that, wrap the complete program in a while loop that is e'er True.
Moreover, add a continue statement at a point where you lot want to beginning the plan from the beginning. You too need to add some code such as a break statement to terminate your programme.
Otherwise, the program volition run infinitely, and we never desire that.
How to loop dorsum in Python ii
Suppose we have a program that takes the distance and time from the user and calculates the speed.
altitude = bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = distance/time print("Speed is:", speed,"kph") Now, we desire to offset from the beginning if the user wants to perform some other adding. To do that, we add together a while statement at the top.
Nosotros also employ a go along statement to restart if the user enters yes. If the user wants to quit, the continue argument will non run, and the program will end. Consider the lawmaking below that implements this.
while True: distance = float(input("Enter the distance in kilometers: ")) fourth dimension = float(input("Enter the time in hours: ")) speed = distance/time print("Speed is:", speed,"kph") check = input("Do you want to quit or start once more? enter Y to restart or some other key to cease: ") if check.upper() == "Y": #go back to the tiptop go along print("Goodbye...") intermission #exit
Looping dorsum in Python Output
2. Using a Function
We can likewise loop back to the beginning by using a role. Instead of wrapping the whole code in a while loop, we create a part and put our plan at that place. If the user wants to continue, nosotros will call the process again. Otherwise, we will exit the program.
Consider the same example implemented using a role.
def repeat():
distance = float(input("Enter the altitude in kilometers: "))
fourth dimension = float(input("Enter the time in hours: "))
speed = distance/time
print("Speed is:", speed,"kph")
check = input("Practise you want to quit or first gain, enter Y to restart or another to end ?: ")
if check.upper() == "Y": #loop back to the beginning
repeat()
impress("Good day...")
get out() #leave the programme
echo() Output
Looping back in Python effect of function arroyo
Hey guys! Information technology'due south me, Marcel, aka Maschi. I earn a total-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these projection whenever I get a risk to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 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 certain that You can get on this path as well! Don't let anyone tell y'all that this can't be done. Sky's the limit, actually…equally long as yous 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 "Python Run Each Loop for Certain Time Before Trying Again"
Post a Comment