Skip to Content

How to Loop Back to the Beginning of a Program in Python

How to Loop Back to the Beginning of a Program in Python

How to Loop Back to the Beginning of a Program in Python?

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 the concept. 

 

Loop back in Python

Loop back in Python

 

In this post, we will talk about two approaches.

 

1. How to Loop Back to the Beginning of a Program in Python Using  a Loop

We can loop back to the start 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 must also add some code, such as a break statement to terminate your program.

Otherwise, the program will run infinitely, and we never desire that.

 

How to loop back in Python 2

How to loop back in Python 2

 

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.

To loop back to the beginning of a program in Python using a loop, we would use this code: 

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

 

Looping back in Python Output

Looping back in Python Output

 

How to Loop Back to the Beginning of a Program in Python 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.

To loop back to the beginning of a program in python using a function, we would use this code: 

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

Looping back in Python result of function approach

Looping back in Python result of function approach

 

Read about ways to loop back to the beginning of a program in Python.

PicoSoft

Saturday 25th of June 2022

Very nice and helpful Thanks from the author of this article ;)

Idrees Jameel

Friday 26th of November 2021

Nice