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
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 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
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 office approach
Read most ways to loop back to the beginning of a program in Python.
Hey guys! It'south me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on superlative of the game! I run several highly assisting blogs & websites and love to speak virtually these project whenever I get a chance to do so. I practise this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-five job and finally got into online business organization every bit a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that Y'all can get on this path as well! Don't let anyone tell yous that this can't be done. Sky's the limit, really…as long as you BELIEVE in it! And it all starts right hither..at Maschituts!
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