How Can I Create a Loop That Goes Back to the Beginning Stage?
Here, we will see how to loop back to the beginning of the program 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 again. Consider the figure below to understand this concept.
In this post, we will talk about two approaches.
1. Using a Loop
We can loop back to the start by using a control flow statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always True.
Moreover, add a continue statement at a point where you want to start the program from the beginning. You also need to add some code such as a break statement to terminate your program.
Otherwise, the program will run infinitely, and we never desire that.
Suppose we have 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 statement at the top.
We also use a continue statement to restart if the user enters yes. If the user wants to quit, the continue statement will not run, and the program will terminate. Consider the code below 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 key to end: ") if check.upper() == "Y": #go back to the top continue print("Bye...") break #exit
2. Using a Function
We can also loop back 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 continue, we will call the procedure again. Otherwise, we will exit the program.
Consider the same example implemented using a function.
def repeat(): 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 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
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 love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-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 like to make sure that YOU can get on this path as well! Don't let anyone tell you that this can't be done. Sky's the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!
How Can I Create a Loop That Goes Back to the Beginning Stage?
Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/
0 Response to "How Can I Create a Loop That Goes Back to the Beginning Stage?"
Postar um comentário