How to Make a Program Run Itself Again

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

Hither, we will see how to loop back to the commencement of the programme in Python. In other words, the program's command is at some point other than the kickoff, and we want the program to get-go from the tiptop again. Consider the figure below to sympathize this concept.

Loop back in Python

Loop back in Python

In this post, we volition talk most 2 approaches.

1. Using a Loop

We tin loop back to the start by using a control flow statement, i.east., a while argument. To do that, wrap the complete program in a while loop that is always True.

Moreover, add together a continue statement at a indicate where you desire to start the program from the beginning. You too demand to add together some code such every bit a intermission 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 altitude and time from the user and calculates the speed.

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

Now, we desire to first from the commencement if the user wants to perform some other calculation. To do that, we add together a while argument at the top.

Nosotros also apply a continue statement to restart if the user enters aye. If the user wants to quit, the continue statement volition not run, and the program will finish. Consider the code beneath that implements this.

while True:   distance =  float(input("Enter the distance in kilometers: "))   time = bladder(input("Enter the time in hours: "))   speed = distance/fourth dimension   print("Speed is:", speed,"kph")   bank check = input("Do y'all want to quit or showtime again? enter Y to restart or some other key to end: ")   if cheque.upper() == "Y": #go back to the top     continue       impress("Adieu...")   break #leave

Looping back in Python Output

Looping back in Python Output

2. Using a Function

We can also loop dorsum to the offset by using a function. Instead of wrapping the whole code in a while loop, we create a role and put our program there. If the user wants to continue, we volition telephone call the procedure 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: "))
   time = bladder(input("Enter the time in hours: "))
   speed = distance/time
     print("Speed is:", speed,"kph")
   cheque = 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 upshot of function approach

Read almost means to loop back to the beginning of a program in Python.

valerioinglacrievor.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"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel