Python How to Run Main Again
How to Loop Back to the Beginning of a Programme in Python?
Here, nosotros will run across how to loop dorsum to the beginning of the programme in Python. In other words, the plan'south command is at some point other than the beginning, and we want the program to start from the superlative again. Consider the figure below to sympathise this concept.
Loop back in Python
In this post, we will talk well-nigh 2 approaches.
one. Using a Loop
We can loop back to the start past using a command flow statement, i.eastward., a while argument. To do that, wrap the complete plan in a while loop that is e'er True.
Moreover, add a proceed argument at a signal where you want to first the program from the beginning. You besides need to add some lawmaking such as a pause statement to terminate your program.
Otherwise, the plan volition run infinitely, and we never want that.
How to loop dorsum in Python 2
Suppose we have a plan that takes the distance and fourth dimension from the user and calculates the speed.
distance = bladder(input("Enter the altitude in kilometers: ")) fourth dimension = 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 some other calculation. To practice that, nosotros add together a while statement at the summit.
We as well utilize a continue statement to restart if the user enters yes. If the user wants to quit, the proceed statement volition not run, and the program will terminate. Consider the code below that implements this.
while True: distance = float(input("Enter the altitude in kilometers: ")) fourth dimension = float(input("Enter the time in hours: ")) speed = altitude/time print("Speed is:", speed,"kph") bank check = input("Practise you desire to quit or showtime again? enter Y to restart or another key to terminate: ") if check.upper() == "Y": #go dorsum to the top go on print("Bye...") break #exit
Looping back in Python Output
2. Using a Function
We can also loop dorsum to the beginning by using a function. Instead of wrapping the whole code in a while loop, we create a function and put our program there. If the user wants to go on, we will call the procedure once again. Otherwise, we volition exit the program.
Consider the aforementioned example implemented using a part.
def echo(): altitude = bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = altitude/time impress("Speed is:", speed,"kph") check = input("Practise you want to quit or start gain, enter Y to restart or some other to end ?: ") if check.upper() == "Y": #loop back to the first repeat() print("Adieu...") exit() #exit the programme repeat()
Output
Looping dorsum in Python result of part approach
Read nearly ways 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 you guys how I stay on top of the game! I run several highly profitable blogs & websites and dearest to speak about these projection whenever I get a adventure to exercise then. I practice this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business organization as a digital entrepreneur, is problably one of the best decisions I e'er took in my life. And I would like to make sure that YOU tin get on this path equally well! Don't let anyone tell you lot that this tin can't be washed. Sky's the limit, actually…as long equally yous BELIEVE in it! And it all starts right hither..at Maschituts!
Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/
Postar um comentário for "Python How to Run Main Again"