How to Run the Code Again Based on Input
Here, we will see how to loop back to the starting time of the plan in Python. In other words, the programme'south control is at some indicate other than the get-go, and we want the programme to showtime from the superlative once again. Consider the figure below to understand this concept.
In this mail, we will talk about ii approaches.
1. Using a Loop
We can loop back to the start by using a command flow statement, i.e., a while statement. To exercise that, wrap the consummate plan in a while loop that is ever True.
Moreover, add a continue argument at a point where you want to start the program from the start. You also need to add some code such as a pause statement to terminate your program.
Otherwise, the plan will run infinitely, and we never desire that.
Suppose we accept a program that takes the distance and time from the user and calculates the speed.
distance = bladder(input("Enter the distance in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = altitude/fourth dimension print("Speed is:", speed,"kph")
Now, we want to start from the beginning if the user wants to perform another adding. To do that, nosotros add together a while argument at the top.
We also utilize a continue statement to restart if the user enters yes. If the user wants to quit, the continue argument will not run, and the program volition end. Consider the lawmaking beneath that implements this.
while True: distance = float(input("Enter the distance in kilometers: ")) time = float(input("Enter the fourth dimension in hours: ")) speed = distance/time print("Speed is:", speed,"kph") check = input("Do you want to quit or start once again? enter Y to restart or another central to end: ") if check.upper() == "Y": #become dorsum to the height keep impress("Bye...") suspension #exit
2. Using a Part
Nosotros tin can besides loop dorsum to the starting time past using a function. Instead of wrapping the whole code in a while loop, we create a part and put our program there. If the user wants to go on, we will call the procedure again. Otherwise, we will exit the plan.
Consider the same example implemented using a function.
def echo(): distance = float(input("Enter the distance in kilometers: ")) fourth dimension = float(input("Enter the time in hours: ")) speed = distance/time print("Speed is:", speed,"kph") check = input("Do you lot desire to quit or start gain, enter Y to restart or another to end ?: ") if check.upper() == "Y": #loop dorsum to the start echo() impress("Bye...") exit() #exit the program repeat()
Output
Hey guys! Information technology'southward 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 love to speak about these project whenever I get a run a risk to do so. I do this total-fourth dimension and wholeheartedly. In fact, the moment I stopped working an eight-to-5 chore and finally got into online business concern as 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 go on this path equally well! Don't permit anyone tell you that this tin't be done. Sky's the limit, really…as long equally yous BELIEVE in it! And it all starts right here..at Maschituts!
Source: https://maschituts.com/2-ways-to-loop-back-to-the-beginning-of-a-program-in-python/
0 Response to "How to Run the Code Again Based on Input"
Enviar um comentário