How to Make a Program Run Itself Again

Here, we will see how to loop back to the start of the programme in Python. In other words, the programme'south control is at some point other than the outset, and nosotros want the plan to start from the meridian again. Consider the effigy below to understand this concept.

Loop back in Python

Loop back in Python

In this post, we will talk virtually two approaches.

1. Using a Loop

We can loop dorsum to the start by using a control period statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always Truthful.

Moreover, add together a go on statement at a point where you want to start the program from the beginning. Y'all also need to add some code such every bit a pause argument to terminate your program.

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

How to loop back in Python 2

How to loop back in Python 2

Suppose nosotros accept 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 argument at the pinnacle.

We also apply a continue argument to restart if the user enters yeah. If the user wants to quit, the proceed statement will non run, and the program will terminate. Consider the lawmaking below that implements this.

while Truthful:   distance =  bladder(input("Enter the distance in kilometers: "))   time = float(input("Enter the time in hours: "))   speed = altitude/time   print("Speed is:", speed,"kph")   bank check = input("Do y'all want to quit or showtime again? enter Y to restart or another central to end: ")   if check.upper() == "Y": #go back to the acme     go on       print("Farewell...")   intermission #exit

Looping back in Python Output

Looping back in Python Output

two. Using a Part

We can too loop dorsum to the get-go by using a function. Instead of wrapping the whole code in a while loop, we create a role and put our program at that place. If the user wants to continue, we will call the procedure again. Otherwise, we will exit the programme.

Consider the same instance implemented using a function.

def echo(): 
  distance =  float(input("Enter the distance in kilometers: "))
   time = float(input("Enter the fourth dimension in hours: "))
   speed = distance/time
     print("Speed is:", speed,"kph")
   check = input("Do you lot want to quit or start gain, enter Y to restart or another to stop ?: ") 
  if check.upper() == "Y": #loop back to the start 
  echo()
  impress("Bye...")
   exit() #go out the program

  repeat()

Output

Looping back in Python result of function approach

Looping back in Python result of office approach

moorehoste1956.blogspot.com

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

0 Response to "How to Make a Program Run Itself Again"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel