How to Incorporate While Loop for Would You Like to Do It Again

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

Here, we volition meet how to loop dorsum to the beginning of the program in Python. In other words, the program'south control is at some point other than the start, and we want the programme to get-go from the top again. Consider the figure below to understand this concept.

Loop back in Python

Loop back in Python

In this post, we volition talk near two approaches.

1. Using a Loop

Nosotros tin loop back to the start past using a control flow statement, i.east., 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 point where you desire to outset the program from the first. You lot as well need to add together some code such as a break argument to terminate your program.

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

How to loop back in Python 2

How to loop back in Python two

Suppose we have a plan that takes the altitude and fourth dimension from the user and calculates the speed.

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

Now, nosotros want to starting time from the beginning if the user wants to perform another calculation. To practise that, nosotros add a while argument at the pinnacle.

We also use a continue statement to restart if the user enters yes. If the user wants to quit, the continue statement will not run, and the program will terminate. Consider the lawmaking below that implements this.

while True:   distance =  float(input("Enter the distance in kilometers: "))   time = float(input("Enter the fourth dimension in hours: "))   speed = distance/fourth dimension   impress("Speed is:", speed,"kph")   check = input("Do you desire to quit or get-go again? enter Y to restart or another key to end: ")   if cheque.upper() == "Y": #get dorsum to the top     continue       print("Goodbye...")   interruption #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Part

We tin can as well loop dorsum to the beginning by using a function. Instead of wrapping the whole lawmaking in a while loop, we create a function and put our plan in that location. If the user wants to continue, nosotros will call the procedure again. Otherwise, we volition go out the plan.

Consider the same instance implemented using a function.

def repeat(): 
  distance =  bladder(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 start proceeds, enter Y to restart or another to end ?: ") 
  if check.upper() == "Y": #loop dorsum to the start 
  repeat()
  print("Farewell...")
   exit() #exit the plan

  echo()

Output

Looping back in Python result of function approach

Looping back in Python result of office approach

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

dockeryacialved.blogspot.com

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

0 Response to "How to Incorporate While Loop for Would You Like to Do It Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel