Mellrose Don t Want to Start Again

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

Here, we volition meet how to loop back to the outset of the programme in Python. In other words, the plan's control is at some betoken other than the beginning, and we want the program to commencement from the height over again. Consider the figure below to sympathise this concept.

Loop back in Python

Loop back in Python

In this post, we will talk about ii approaches.

one. Using a Loop

We tin loop back to the start by using a control flow argument, i.due east., a while statement. To do that, wrap the complete plan in a while loop that is always True.

Moreover, add a go along statement at a signal where you desire to start the program from the beginning. You as well need to add together some code such as a break statement to terminate your program.

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

How to loop back in Python 2

How to loop back in Python ii

Suppose nosotros have a programme that takes the altitude and time from the user and calculates the speed.

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")

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

Nosotros also use a continue argument to restart if the user enters yeah. If the user wants to quit, the keep argument will non run, and the programme will cease. Consider the code below that implements this.

while Truthful:   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 yous desire to quit or get-go once again? enter Y to restart or some other key to end: ")   if cheque.upper() == "Y": #go back to the top     proceed       impress("Adieu...")   break #exit

Looping back in Python Output

Looping back in Python Output

2. Using a Function

We can besides loop back 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, we will call the procedure again. Otherwise, we will exit the program.

Consider the same example implemented using a role.

def repeat(): 
  distance =  float(input("Enter the distance in kilometers: "))
   fourth dimension = float(input("Enter the fourth dimension in hours: "))
   speed = distance/time
     impress("Speed is:", speed,"kph")
   check = input("Do yous want to quit or commencement proceeds, enter Y to restart or another to stop ?: ") 
  if bank check.upper() == "Y": #loop back to the start 
  repeat()
  impress("Good day...")
   exit() #exit the program

  repeat()

Output

Looping back in Python result of function approach

Looping back in Python event of part approach

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

phillipstimenot.blogspot.com

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

0 Response to "Mellrose Don t Want to Start Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel