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.
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.
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
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
Read about ways to loop back to the beginning of a program in Python.
Hey guys! Information technology's me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and dearest to speak nearly these projection whenever I go a chance to practise so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-five job and finally got into online business as a digital entrepreneur, is problably ane of the all-time decisions I ever took in my life. And I would like to brand sure that Y'all can get on this path as well! Don't allow anyone tell you that this tin't be done. Sky's the limit, really…as long as you lot BELIEVE in it! And information technology all starts correct here..at Maschituts!
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