Code Example: Loop Back To The Beginning Of A Program

Are you using Python and want to loop back to the beginning of a program? In this article, I will discuss the different aspects of Looping back to the start of the program.

You are going to learn the below things in this tutorial.

  • Loop Back to Beginning of A Program
  • How To Make a Python Program Repeat Itself
  • How To Loop Back To the beginning of a Program in Python
  • How To Go Back to Previous input in Python

So, the basic solution to all the problem statements mentioned above is using a while[1] loop. A while loop in a program can keep your program active as long as you want. And once you have your condition to end the program reaches it can kill itself.

How To Loop Back To The Beginning Of A Program – Using While Loop

Suppose you have created a program that asks users input for a few options as you get in your mobile phone when you call the customer care number. Now since you want your program to run unless the user exit itself, in that case, you need to use the while loop.

Let us see in the below example code the usage of the while conditional operator to loop back to the beginning of the program using Python.

#Setting Program To Continue to Run Unless
#Condition to Exit is hit
while True:
    #Asking if the user want to restart the program 
    #Or Exit the Program
    again = input("Do you want to Exit this program.")
    if again not in {"y","n"}:
        print("Enter y to exit or n to continue.")               
    elif again == "n":
        #Keep Restarting the Program
        print("Restarting the Program\n")
    elif again == "y":
        # Exit the Program
        print("Exiting the Program..")
        break

Output:

Do you want to Exit this program.n
Restarting the Program

Do you want to Exit this program.n
Restarting the Program

Do you want to Exit this program.y
Exiting the Program..

As you can see from the above program that using the while loop unless the user enters the option as “y” the program continues to run and keeps on asking the user the question.

And once the user enters the “y” the program break out of the while loop ending the program to run further. Similarly, you can use this code to continue running your code unless you receive an exit program code from the user.

You can use the same code as mentioned above to loop back to previous input in Python. There is no difference in approach to getting that problem statement resolved.

How To Make Python Program To Repeat Itself

If you want a program in Python to repeat itself unless it receives or satisfies the exit condition then in that case you can use that program in a function and then you can create another function with a while loop to keep on calling the Python program unless the exit condition is met.

Let us see in the code example how to make a Python program repeat itself in Python.

def runProgramToRepeat():
    #If keepReapeating Remains True keep 
    #Repeating the Program
    keepRepeating = 1
    while keepRepeating:
        keepRepeating = programToRepeat()
    


def programToRepeat():
    again = input("Do you want to Exit this program.")
    if again not in {"y","n"}:
        print("Enter y or n.")               
    elif again == "n":
        print("Restarting the Program\n")
        return 1
    elif again == "y":
        # Exit the Program
        print("Exiting the Program..")
        return 0

runProgramToRepeat()

Output:

Do you want to Exit this program.n
Restarting the Program

Do you want to Exit this program.n
Restarting the Program

Do you want to Exit this program.y
Exiting the Program..  

As you can see in the above code, I was able to create a function that repeats itself to the beginning of the program. And hence it completes our purpose of making a program that repeats itself.

I am keep checking the return from the function programToRepeat() and once it returns 0 then the program understands that it need not repeat the program from the beginning and it should exit now.

You can use the same code mentioned above to loop back to the beginning of a program in Python. There is no such difference and it can be achieved using the same codes mentioned above.

Code Example: Loop Back To The Beginning Of A Programme

Wrap Up

I hope you were able to learn how to loop back to the beginning of the program, repeat the program itself in Python, How To Loop Back To the beginning of a Program in Python, How To Go Back to Previous input in Python. I have listed out two methods that you can use to resolve the above problem statements.

Let me know in the comments section if you have any better method than the one discussed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Do Nothing In An If Statement Python
  2. Code Example: Remove The First Item From List Python
  3. 4 Ways To Loop Through List With Index Using Python
  4. Code Examples: Multiply In Python
  5. How To Join Words In a List Using Python Code Example
  6. Python Code Example: How To Find Element in a List in 6 Way
  7. How To Test For An Empty Object In JavaScript
  8. How To Iterate Over Rows in a DataFrame in Pandas Python
  9. Python Get Filename From A Path Without Extension 4 Ways
  10. How To Concatenate String and Integer In Python
Code Example: Loop Back To The Beginning Of A Program

Leave a Comment