How to Run the Code Again Based on Input

Here, we will see how to loop back to the starting time of the plan in Python. In other words, the programme'south control is at some indicate other than the get-go, and we want the programme to showtime from the superlative once again. Consider the figure below to understand this concept.

Loop back in Python

Loop dorsum in Python

In this mail, we will talk about ii approaches.

1. Using a Loop

We can loop back to the start by using a command flow statement, i.e., a while statement. To exercise that, wrap the consummate plan in a while loop that is ever True.

Moreover, add a continue argument at a point where you want to start the program from the start. You also need to add some code such as a pause statement to terminate your program.

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

How to loop back in Python 2

How to loop back in Python two

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

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

Now, we want to start from the beginning if the user wants to perform another adding. To do that, nosotros add together a while argument at the top.

We also utilize a continue statement to restart if the user enters yes. If the user wants to quit, the continue argument will not run, and the program volition end. Consider the lawmaking beneath that implements this.

while True:   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 want to quit or start once again? enter Y to restart or another central to end: ")   if check.upper() == "Y": #become dorsum to the height     keep       impress("Bye...")   suspension #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Part

Nosotros tin can besides loop dorsum to the starting time past using a function. Instead of wrapping the whole code in a while loop, we create a part and put our program there. If the user wants to go on, we will call the procedure again. Otherwise, we will exit the plan.

Consider the same example implemented using a function.

def echo(): 
  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 lot desire to quit or start gain, enter Y to restart or another to end ?: ") 
  if check.upper() == "Y": #loop dorsum to the start 
  echo()
  impress("Bye...")
   exit() #exit the program

  repeat()

Output

Looping back in Python result of function approach

Looping dorsum in Python result of function arroyo

robisonthatithe.blogspot.com

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

0 Response to "How to Run the Code Again Based on Input"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel