How to Ask for User Input to Run Program Again

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

Here, we will see how to loop back to the beginning of the program in Python. In other words, the program'southward command is at some bespeak other than the beginning, and we want the program to kickoff from the top again. Consider the figure below to understand this concept.

Loop back in Python

Loop back in Python

In this post, we will talk most two approaches.

1. Using a Loop

We can loop back to the kickoff by using a command flow statement, i.e., a while statement. To practice that, wrap the complete program in a while loop that is always True.

Moreover, add together a continue statement at a betoken where you desire to start the program from the first. You also need to add together some code such as a intermission argument to terminate your program.

Otherwise, the programme volition run infinitely, and we never want that.

How to loop back in Python 2

How to loop back in Python 2

Suppose we take 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")

At present, we want to offset from the beginning if the user wants to perform some other calculation. To exercise that, we add a while statement at the acme.

We too utilize a continue statement to restart if the user enters aye. If the user wants to quit, the go on statement will not run, and the program will terminate. Consider the code below that implements this.

while True:   distance =  bladder(input("Enter the distance in kilometers: "))   time = bladder(input("Enter the fourth dimension in hours: "))   speed = distance/time   impress("Speed is:", speed,"kph")   check = input("Do y'all want to quit or showtime again? enter Y to restart or another key to cease: ")   if check.upper() == "Y": #go dorsum to the top     go along       print("Bye...")   pause #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Function

We can as well loop back to the first past using a office. Instead of wrapping the whole code in a while loop, nosotros create a function and put our program there. If the user wants to continue, we volition call the procedure again. Otherwise, nosotros will exit the program.

Consider the same case implemented using a role.

def repeat(): 
  distance =  float(input("Enter the distance in kilometers: "))
   time = bladder(input("Enter the time in hours: "))
   speed = altitude/time
     print("Speed is:", speed,"kph")
   check = input("Do you desire to quit or showtime gain, enter Y to restart or another to cease ?: ") 
  if check.upper() == "Y": #loop back to the start 
  repeat()
  impress("Cheerio...")
   exit() #exit the program

  repeat()

Output

Looping back in Python result of function approach

Looping back in Python issue of function approach

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

mckinneysymee1942.blogspot.com

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

0 Response to "How to Ask for User Input to Run Program Again"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel