Python How to Run Main Again
How to Loop Dorsum to the Beginning of a Program in Python?
Here, we will come across how to loop back to the beginning of the programme in Python. In other words, the program's control is at some point other than the beginning, and we want the program to start from the top once more. Consider the figure below to understand this concept.
Loop back in Python
In this post, we will talk about two approaches.
1. Using a Loop
We can loop dorsum to the start past using a command flow argument, i.e., a while statement. To do that, wrap the consummate programme in a while loop that is always Truthful.
Moreover, add a continue statement at a point where you want to start the program from the start. Yous too need to add some code such as a break statement to terminate your programme.
Otherwise, the program will run infinitely, and we never want that.
How to loop back in Python 2
Suppose we have a program that takes the altitude and time from the user and calculates the speed.
altitude = float(input("Enter the distance in kilometers: ")) time = bladder(input("Enter the fourth dimension in hours: ")) speed = distance/fourth dimension print("Speed is:", speed,"kph") Now, nosotros desire to start from the beginning if the user wants to perform some other calculation. To do that, we add a while statement at the summit.
We likewise use a continue statement to restart if the user enters yes. If the user wants to quit, the go along statement will not run, and the plan will stop. Consider the code beneath that implements this.
while True: distance = float(input("Enter the distance in kilometers: ")) time = float(input("Enter the time in hours: ")) speed = distance/time print("Speed is:", speed,"kph") check = input("Do you want to quit or start again? enter Y to restart or another central to end: ") if check.upper() == "Y": #go dorsum to the height continue print("Bye...") break #exit
Looping back in Python Output
2. Using a Office
Nosotros tin can also loop back to the showtime by using a part. Instead of wrapping the whole lawmaking in a while loop, we create a part and put our plan there. If the user wants to go on, we will telephone call the procedure again. Otherwise, nosotros will exit the programme.
Consider the same case implemented using a function.
def repeat():
distance = float(input("Enter the altitude in kilometers: "))
time = float(input("Enter the time in hours: "))
speed = distance/time
print("Speed is:", speed,"kph")
check = input("Do you want to quit or first gain, enter Y to restart or another to end ?: ")
if check.upper() == "Y": #loop back to the start
echo()
print("Farewell...")
exit() #go out the program
repeat() Output
Looping back in Python effect of office approach
Read well-nigh ways to loop back to the showtime 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 dearest to speak about these project whenever I go a chance to do so. I do this total-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 task and finally got into online business organization as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to brand sure that YOU can get on this path likewise! Don't let anyone tell y'all that this tin 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 "Python How to Run Main Again"
Post a Comment