Python Run Each Loop for Certain Time Before Trying Again

Here, we will see how to loop back to the beginning of the program in Python. In other words, the program'south control is at some point other than the starting time, and we desire the programme to showtime from the superlative again. Consider the figure below to understand this concept.

Loop back in Python

Loop back in Python

In this post, nosotros will talk most two approaches.

one. Using a Loop

We can loop dorsum to the start past using a command menses argument, i.e., a while argument. To practice that, wrap the complete program in a while loop that is e'er True.

Moreover, add a continue statement at a point where you lot want to beginning the plan from the beginning. You too need to add some code such as a break statement to terminate your programme.

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

How to loop back in Python 2

How to loop dorsum in Python ii

Suppose we have a program that takes the distance and time from the user and calculates the speed.

altitude =  bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = distance/time print("Speed is:", speed,"kph")

Now, we desire to offset from the beginning if the user wants to perform some other adding. To do that, we add together a while statement at the top.

Nosotros also employ a go along statement to restart if the user enters yes. If the user wants to quit, the continue argument will non run, and the program will end. Consider the lawmaking below that implements this.

while True:   distance =  float(input("Enter the distance in kilometers: "))   fourth dimension = float(input("Enter the time in hours: "))   speed = distance/time   print("Speed is:", speed,"kph")   check = input("Do you want to quit or start once more? enter Y to restart or some other key to cease: ")   if check.upper() == "Y": #go back to the tiptop     go along       print("Goodbye...")   intermission #exit

Looping back in Python Output

Looping dorsum in Python Output

2. Using a Function

We can likewise loop back to the beginning by using a role. Instead of wrapping the whole code in a while loop, we create a part and put our plan at that place. If the user wants to continue, nosotros will call the process again. Otherwise, we will exit the program.

Consider the same example implemented using a role.

def repeat(): 
  distance =  float(input("Enter the altitude in kilometers: "))
   fourth dimension = float(input("Enter the time in hours: "))
   speed = distance/time
     print("Speed is:", speed,"kph")
   check = input("Practise you want to quit or first gain, enter Y to restart or another to end ?: ") 
  if check.upper() == "Y": #loop back to the beginning 
  repeat()
  impress("Good day...")
   get out() #leave the programme

  echo()

Output

Looping back in Python result of function approach

Looping back in Python effect of function arroyo

williamsfley1989.blogspot.com

Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/

0 Response to "Python Run Each Loop for Certain Time Before Trying Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel