How to Make a for Loop Run Again From if Inside It

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

Here, we will encounter how to loop dorsum to the outset of the programme in Python. In other words, the program'due south command is at some point other than the beginning, and we want the program to start from the height again. Consider the effigy below to understand this concept.

Loop back in Python

Loop dorsum in Python

In this postal service, we will talk most 2 approaches.

1. Using a Loop

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

Moreover, add a go along statement at a bespeak where you lot want to showtime the plan from the commencement. You also need to add some code such every bit a interruption statement to terminate your programme.

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

How to loop back in Python 2

How to loop back in Python ii

Suppose we take a programme that takes the altitude and time from the user and calculates the speed.

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

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

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

while True:   distance =  float(input("Enter the distance in kilometers: "))   fourth dimension = float(input("Enter the time in hours: "))   speed = distance/fourth dimension   print("Speed is:", speed,"kph")   cheque = input("Practise you lot desire to quit or start again? enter Y to restart or another key to finish: ")   if cheque.upper() == "Y": #go back to the peak     go on       impress("Bye...")   break #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Part

We can besides loop dorsum to the offset by using a function. Instead of wrapping the whole lawmaking in a while loop, we create a part and put our plan at that place. If the user wants to go along, we volition call the procedure once more. Otherwise, we will exit the plan.

Consider the same example implemented using a function.

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

  echo()

Output

Looping back in Python result of function approach

Looping dorsum in Python result of function approach

Read almost ways to loop back to the beginning of a programme in Python.

rodriguezhiselon.blogspot.com

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

Belum ada Komentar untuk "How to Make a for Loop Run Again From if Inside It"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel