How do i go back to a step 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 this concept.

How do i go back to a step in python?

Loop back in Python

How do i go back to a step in python?

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.

How do i go back to a step in python?

How to loop back in Python 2

How do i go back to a step in python?

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

How do i go back to a step in python?

How do i go back to a step in python?

Looping back in Python Output

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

How do i go back to a step in python?

Looping back in Python result of function approach

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

How do i go back to a step in python?

How do i go back to a step in python?

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 do you go back to a specific point in Python?

To go back to a point, simply put a large while loop around the stuff you want to repeat. You already know how to do that.

How do you jump back to a specific line in Python?

Python does not allow you to go back to a specific line number, and even if it did, you should not take advantage of that capability because it results in unmaintainable programs. Instead, learn how to use functions and structure your code so that functions make sense.

How do you go back a step in a for loop Python?

for loops don't go back or forth. They simply take the iterator object for a given iterable object, then repeatedly call the __next__() object until that method raises StopIteration .