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.
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.
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
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
Read almost ways to loop back to the beginning of a programme in Python.
Hey guys! It's me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with yous guys how I stay on top of the game! I run several highly profitable blogs & websites and dearest to speak virtually these project whenever I get a chance to exercise then. I practice this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the all-time decisions I ever took in my life. And I would like to brand certain that You lot tin can get on this path likewise! Don't let anyone tell you that this can't be done. Sky'south the limit, really…as long as you BELIEVE in it! And information technology all starts right hither..at Maschituts!
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