Text Menu With Infinite Loop in Python

Aditya Raj Jul 12, 2022
  1. Text Menu With Infinite Loop in Python
  2. Terminate Text Menu With Infinite Loop Using the break Statement in Python
  3. Terminate Text Menu With Infinite Loop Using the Flag Variable in Python
  4. Conclusion
Text Menu With Infinite Loop in Python

We can use the while loop and if-else statements to implement various tools in a Python program. This article will create a text menu with an infinite loop in Python.

Text Menu With Infinite Loop in Python

We will use a while loop with conditional statements to create a text menu with an infinite loop. Inside the while loop, we will first show the user several options, and after showing the options, we will take input from the user.

After taking the input, the program will print the desired output. Finally, the program will print the options.

This will continue until the program is terminated manually by the user.

To understand this, consider the following program.

def options():
    print("Enter 1 to print 'Hi'.")
    print("Enter 2 to print 'Hello'.")
    print("Enter 3 to print 'Namaste'.")
    print("Enter 4 to print 'Bonjour'.")
    print("Enter 5 to print 'Hola'.")


while True:
    options()
    option = int(input())
    if option == 1:
        print("Hi")
    elif option == 2:
        print("Hello")
    elif option == 3:
        print("Namaste")
    elif option == 4:
        print("Bonjour")
    elif option == 5:
        print("Hola")

Output:

Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
1
Hi
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
2
Hello
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
^D
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 11, in <module>
    option = int(input())
EOFError: EOF when reading a line

In the above code, we have first defined the function options() to print various options available to the user. After that, we created a while loop.

Inside the while loop, we first executed the options() function. After that, we asked the user to enter a number.

Subsequently, we converted the input into an integer using the int() function because the input() function returned a string.

The program printed a message according to the input. After that, the program again displayed the options.

This continued until the user manually terminated the program.

As discussed in the following sections, we can use several ways to terminate the while loop to advance into the program.

Terminate Text Menu With Infinite Loop Using the break Statement in Python

We will ask the user to press any number other than the given options to terminate the while loop. After that, we will include an else block in the conditional statements.

If the user enters any other number than the specified options, we will print Bye and move out of the while loop using the break statement, as shown in the following example.

def options():
    print("Enter 1 to print 'Hi'.")
    print("Enter 2 to print 'Hello'.")
    print("Enter 3 to print 'Namaste'.")
    print("Enter 4 to print 'Bonjour'.")
    print("Enter 5 to print 'Hola'.")
    print("Enter any other number to terminate.")


while True:
    options()
    option = int(input())
    if option == 1:
        print("Hi")
    elif option == 2:
        print("Hello")
    elif option == 3:
        print("Namaste")
    elif option == 4:
        print("Bonjour")
    elif option == 5:
        print("Hola")
    else:
        print("Bye")
        break

Output:

Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
1
Hi
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
3
Namaste
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
12
Bye

In the above example, when the user enters any number other than 1 to 5, the program execution enters the else block of the conditional statements. Therefore, the program prints Bye, and the break statement is executed.

Due to this, the execution of the program moves out of the while loop.

Terminate Text Menu With Infinite Loop Using the Flag Variable in Python

Instead of using the break statement, we can use a flag variable to control the execution of the infinite loop. First, we will initialize a flag variable to True before executing the while loop.

Then, we will execute the while loop if the flag variable is True. Inside the while loop, if the user enters any other number than the specified options, we will print Bye and assign the value False to the flag variable.

Once the flag variable becomes False, the while loop execution will automatically terminate. You can observe this in the following code.

def options():
    print("Enter 1 to print 'Hi'.")
    print("Enter 2 to print 'Hello'.")
    print("Enter 3 to print 'Namaste'.")
    print("Enter 4 to print 'Bonjour'.")
    print("Enter 5 to print 'Hola'.")
    print("Enter any other number to terminate.")


flag = True
while flag:
    options()
    option = int(input())
    if option == 1:
        print("Hi")
    elif option == 2:
        print("Hello")
    elif option == 3:
        print("Namaste")
    elif option == 4:
        print("Bonjour")
    elif option == 5:
        print("Hola")
    else:
        print("Bye")
        flag = False

Output:

Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
1
Hi
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
3
Namaste
Enter 1 to print 'Hi'.
Enter 2 to print 'Hello'.
Enter 3 to print 'Namaste'.
Enter 4 to print 'Bonjour'.
Enter 5 to print 'Hola'.
Enter any other number to terminate.
12
Bye

In the above example, once the flag variable becomes False, the execution of the while loop is terminated. This is because the Python interpreter first checks if the flag variable contains the value True; if yes, then only the while loop is executed.

Once we assign the value False to the flag variable, the interpreter checks the value in the flag variable before the next execution of the while loop. Upon seeing that the value in the flag variable is False, it terminates the while loop.

Conclusion

In this article, we created a text menu with an infinite loop using the while loop and conditional statements in Python. We also discussed how we could terminate the execution of the infinite loop using a break statement and a flag variable.

Related Article - Python Loop