How to Make a Program Run Itself Again
How to Loop Dorsum to the Beginning of a Program in Python?
Hither, we will see how to loop back to the commencement of the programme in Python. In other words, the program's command is at some point other than the kickoff, and we want the program to get-go from the tiptop again. Consider the figure below to sympathize this concept.
In this post, we volition talk most 2 approaches.
1. Using a Loop
We tin loop back to the start by using a control flow statement, i.east., a while argument. To do that, wrap the complete program in a while loop that is always True.
Moreover, add together a continue statement at a indicate where you desire to start the program from the beginning. You too demand to add together some code such every bit a intermission argument to terminate your program.
Otherwise, the programme will run infinitely, and we never desire that.
Suppose nosotros accept a program that takes the altitude and time from the user and calculates the speed.
distance = float(input("Enter the distance in kilometers: ")) fourth dimension = float(input("Enter the time in hours: ")) speed = distance/fourth dimension print("Speed is:", speed,"kph")
Now, we desire to first from the commencement if the user wants to perform some other calculation. To do that, we add together a while argument at the top.
Nosotros also apply a continue statement to restart if the user enters aye. If the user wants to quit, the continue statement volition not run, and the program will finish. Consider the code beneath that implements this.
while True: distance = float(input("Enter the distance in kilometers: ")) time = bladder(input("Enter the time in hours: ")) speed = distance/fourth dimension print("Speed is:", speed,"kph") bank check = input("Do y'all want to quit or showtime again? enter Y to restart or some other key to end: ") if cheque.upper() == "Y": #go back to the top continue impress("Adieu...") break #leave
2. Using a Function
We can also loop dorsum to the offset by using a function. Instead of wrapping the whole code in a while loop, we create a role and put our program there. If the user wants to continue, we volition telephone call the procedure 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: ")) time = bladder(input("Enter the time in hours: ")) speed = distance/time print("Speed is:", speed,"kph") cheque = input("Do you want to quit or start gain, enter Y to restart or another to end ?: ") if check.upper() == "Y": #loop back to the start repeat() print("Bye...") exit() #exit the program repeat()
Output
Read almost means to loop back to the beginning of a program in Python.
Hey guys! Information technology's me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with yous 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 become a chance to do and then. I practise this total-time and wholeheartedly. In fact, the moment I stopped working an viii-to-5 job 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 similar to brand sure that You lot tin can go on this path too! Don't let anyone tell you that this can't be done. Heaven'south the limit, really…as long as you BELIEVE in it! And it all starts right hither..at Maschituts!
valerioinglacrievor.blogspot.com
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"
Post a Comment