Python How to Run Main Again

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

Here, we will come across how to loop back to the beginning of the programme in Python. In other words, the program's control is at some point other than the beginning, and we want the program to start from the top once more. Consider the figure below to understand this concept.

Loop back in Python

Loop back in Python

In this post, we will talk about two approaches.

1. Using a Loop

We can loop dorsum to the start past using a command flow argument, i.e., a while statement. To do that, wrap the consummate programme in a while loop that is always Truthful.

Moreover, add a continue statement at a point where you want to start the program from the start. Yous too need to add some code such as a break statement to terminate your programme.

Otherwise, the program will run infinitely, and we never want that.

How to loop back in Python 2

How to loop back in Python 2

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

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

Now, nosotros desire to start from the beginning if the user wants to perform some other calculation. To do that, we add a while statement at the summit.

We likewise use a continue statement to restart if the user enters yes. If the user wants to quit, the go along statement will not run, and the plan will stop. Consider the code beneath that implements this.

while True:   distance =  float(input("Enter the distance in kilometers: "))   time = float(input("Enter the time in hours: "))   speed = distance/time   print("Speed is:", speed,"kph")   check = input("Do you want to quit or start again? enter Y to restart or another central to end: ")   if check.upper() == "Y": #go dorsum to the height     continue       print("Bye...")   break #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Office

Nosotros tin can also loop back to the showtime by using a part. Instead of wrapping the whole lawmaking in a while loop, we create a part and put our plan there. If the user wants to go on, we will telephone call the procedure again. Otherwise, nosotros will exit the programme.

Consider the same case implemented using a function.

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

  repeat()

Output

Looping back in Python result of function approach

Looping back in Python effect of office approach

Read well-nigh ways to loop back to the showtime of a program in Python.

arrowoodhors1995.blogspot.com

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

0 Response to "Python How to Run Main Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel