Skip to content Skip to sidebar Skip to footer

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

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 back in Python 2

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

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 back in Python result of function approach

Looping dorsum in Python result of part approach

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

woodsstor1949.blogspot.com

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"