How to Make a Program Run Itself Again
Here, we will see how to loop back to the start of the programme in Python. In other words, the programme'south control is at some point other than the outset, and nosotros want the plan to start from the meridian again. Consider the effigy below to understand this concept.
Loop back in Python
In this post, we will talk virtually two approaches.
1. Using a Loop
We can loop dorsum to the start by using a control period statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always Truthful.
Moreover, add together a go on statement at a point where you want to start the program from the beginning. Y'all also need to add some code such every bit a pause argument to terminate your program.
Otherwise, the programme will run infinitely, and we never desire that.
How to loop back in Python 2
Suppose nosotros accept a program that takes the distance and time from the user and calculates the speed.
distance = float(input("Enter the distance in kilometers: ")) time = float(input("Enter the time in hours: ")) speed = distance/time print("Speed is:", speed,"kph") Now, we want to start from the beginning if the user wants to perform another calculation. To do that, we add a while argument at the pinnacle.
We also apply a continue argument to restart if the user enters yeah. If the user wants to quit, the proceed statement will non run, and the program will terminate. Consider the lawmaking below that implements this.
while Truthful: distance = bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the time in hours: ")) speed = altitude/time print("Speed is:", speed,"kph") bank check = input("Do y'all want to quit or showtime again? enter Y to restart or another central to end: ") if check.upper() == "Y": #go back to the acme go on print("Farewell...") intermission #exit
Looping back in Python Output
two. Using a Part
We can too loop dorsum to the get-go by using a function. Instead of wrapping the whole code in a while loop, we create a role and put our program at that place. If the user wants to continue, we will call the procedure again. Otherwise, we will exit the programme.
Consider the same instance implemented using a function.
def echo():
distance = float(input("Enter the distance in kilometers: "))
time = float(input("Enter the fourth dimension in hours: "))
speed = distance/time
print("Speed is:", speed,"kph")
check = input("Do you lot want to quit or start gain, enter Y to restart or another to stop ?: ")
if check.upper() == "Y": #loop back to the start
echo()
impress("Bye...")
exit() #go out the program
repeat() Output
Looping back in Python result of office approach
Hey guys! It's me, Marcel, aka Maschi. I earn a full-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 dearest to speak almost these project whenever I get a chance to do and then. I do this full-time and wholeheartedly. In fact, the moment I stopped working an viii-to-5 job and finally got into online business organization as a digital entrepreneur, is problably one of the all-time decisions I always took in my life. And I would like to make certain that YOU tin can get on this path as well! Don't let anyone tell you that this can't be done. Heaven'due south the limit, actually…as long every bit yous BELIEVE in it! And it all starts correct 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 Make a Program Run Itself Again"
Postar um comentário