How to Ask for User Input to Run Program Again
How to Loop Dorsum to the Beginning of a Program in Python?
Here, we will see how to loop back to the beginning of the program in Python. In other words, the program'southward command is at some bespeak other than the beginning, and we want the program to kickoff from the top again. Consider the figure below to understand this concept.
In this post, we will talk most two approaches.
1. Using a Loop
We can loop back to the kickoff by using a command flow statement, i.e., 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 betoken where you desire to start the program from the first. You also need to add together some code such as a intermission argument to terminate your program.
Otherwise, the programme volition run infinitely, and we never want that.
Suppose we take a program that takes the distance and time 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")
At present, we want to offset from the beginning if the user wants to perform some other calculation. To exercise that, we add a while statement at the acme.
We too utilize a continue statement to restart if the user enters aye. If the user wants to quit, the go on statement will not run, and the program will terminate. Consider the code below that implements this.
while True: distance = bladder(input("Enter the distance in kilometers: ")) time = bladder(input("Enter the fourth dimension in hours: ")) speed = distance/time impress("Speed is:", speed,"kph") check = input("Do y'all want to quit or showtime again? enter Y to restart or another key to cease: ") if check.upper() == "Y": #go dorsum to the top go along print("Bye...") pause #exit
2. Using a Function
We can as well loop back to the first past using a office. Instead of wrapping the whole code in a while loop, nosotros create a function and put our program there. If the user wants to continue, we volition call the procedure again. Otherwise, nosotros will exit the program.
Consider the same case implemented using a role.
def repeat(): distance = float(input("Enter the distance in kilometers: ")) time = bladder(input("Enter the time in hours: ")) speed = altitude/time print("Speed is:", speed,"kph") check = input("Do you desire to quit or showtime gain, enter Y to restart or another to cease ?: ") if check.upper() == "Y": #loop back to the start repeat() impress("Cheerio...") exit() #exit the program repeat()
Output
Read most ways to loop back to the beginning of a program in Python.
Hey guys! Information technology'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 pinnacle of the game! I run several highly profitable blogs & websites and love to speak well-nigh these project whenever I get a run a risk to practice and so. I do this total-time and wholeheartedly. In fact, the moment I stopped working an eight-to-5 job and finally got into online business equally a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path besides! Don't let anyone tell you that this can't exist done. Sky's the limit, really…as long every bit you BELIEVE in it! And it all starts correct here..at Maschituts!
mckinneysymee1942.blogspot.com
Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/
0 Response to "How to Ask for User Input to Run Program Again"
Enregistrer un commentaire