Python Play Again Loop Using While

How to Loop Back to the Kickoff of a Programme in Python?

Hither, we will see how to loop back to the beginning of the plan in Python. In other words, the program's command is at some point other than the beginning, and we want the program to start from the superlative again. Consider the figure below to understand this concept.

Loop back in Python

Loop back in Python

In this postal service, nosotros will talk almost two approaches.

1. Using a Loop

We tin loop dorsum to the start past using a control catamenia argument, i.eastward., a while statement. To do that, wrap the consummate program in a while loop that is always Truthful.

Moreover, add a continue argument at a betoken where you want to start the program from the start. You also need to add some lawmaking such as a break statement to end your plan.

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

How to loop back in Python 2

How to loop dorsum in Python 2

Suppose we have a program that takes the distance and fourth dimension 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")

Now, we desire to start from the beginning if the user wants to perform another calculation. To practise that, we add a while statement at the top.

We too use a continue argument to restart if the user enters yes. If the user wants to quit, the continue statement will non run, and the plan will cease. Consider the code below that implements this.

while True:   distance =  bladder(input("Enter the distance in kilometers: "))   time = bladder(input("Enter the time in hours: "))   speed = distance/time   print("Speed is:", speed,"kph")   check = input("Do you lot want to quit or beginning again? enter Y to restart or some other primal to end: ")   if cheque.upper() == "Y": #go back to the top     continue       print("Bye...")   break #leave

Looping back in Python Output

Looping back in Python Output

two. Using a Function

We tin can also loop back to the starting time by using a office. Instead of wrapping the whole lawmaking in a while loop, we create a function and put our programme there. If the user wants to continue, nosotros will call the procedure again. Otherwise, we will get out the program.

Consider the same instance implemented using a role.

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

  repeat()

Output

Looping back in Python result of function approach

Looping dorsum in Python result of function approach

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

stoatedrund1965.blogspot.com

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

0 Response to "Python Play Again Loop Using While"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel