Python 中帶有無限迴圈的文字選單

Aditya Raj 2023年1月30日
  1. Python 中帶有無限迴圈的文字選單
  2. 使用 Python 中的 break 語句以無限迴圈終止文字選單
  3. 使用 Python 中的 Flag 變數以無限迴圈終止文字選單
  4. まとめ
Python 中帶有無限迴圈的文字選單

我們可以使用 while 迴圈和 if-else 語句在 Python 程式中實現各種工具。本文將在 Python 中建立一個帶有無限迴圈的文字選單。

Python 中帶有無限迴圈的文字選單

我們將使用帶有條件語句的 while 迴圈來建立一個帶有無限迴圈的文字選單。在 while 迴圈中,我們將首先向使用者顯示幾個選項,在顯示選項後,我們將接受使用者的輸入。

接受輸入後,程式將列印所需的輸出。最後,程式將列印選項。

這將一直持續到使用者手動終止程式。

要理解這一點,請考慮以下程式。

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")

輸出:

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

在上面的程式碼中,我們首先定義了函式 options() 來列印使用者可用的各種選項。之後,我們建立了一個 while 迴圈。

while 迴圈中,我們首先執行 options() 函式。之後,我們要求使用者輸入一個數字。

隨後,我們使用 int() 函式將輸入轉換為整數,因為 input() 函式返回一個字串。

程式根據輸入列印一條訊息。之後,程式再次顯示選項。

這種情況一直持續到使用者手動終止程式。

正如以下部分所討論的,我們可以使用幾種方法來終止 while 迴圈以進入程式。

使用 Python 中的 break 語句以無限迴圈終止文字選單

我們將要求使用者按給定選項以外的任何數字來終止 while 迴圈。之後,我們將在條件語句中包含一個 else 塊。

如果使用者輸入指定選項以外的任何其他數字,我們將列印 Bye 並使用 break 語句移出 while 迴圈,如下例所示。

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

輸出:

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

在上面的例子中,當使用者輸入 1 到 5 以外的任何數字時,程式執行進入條件語句的 else 塊。因此,程式列印 Bye,並執行 break 語句。

因此,程式的執行移出 while 迴圈。

使用 Python 中的 Flag 變數以無限迴圈終止文字選單

除了使用 break 語句,我們可以使用 flag 變數來控制無限迴圈的執行。首先,我們將在執行 while 迴圈之前將 flag 變數初始化為 True

然後,如果 flag 變數為 True,我們將執行 while 迴圈。在 while 迴圈中,如果使用者輸入了指定選項以外的任何其他數字,我們將列印 Bye 並將值 False 分配給 flag 變數。

一旦 flag 變數變為 Falsewhile 迴圈執行將自動終止。你可以在以下程式碼中觀察到這一點。

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

輸出:

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

在上面的示例中,一旦 flag 變數變為 Falsewhile 迴圈的執行將終止。這是因為 Python 直譯器首先檢查 flag 變數是否包含值 True;如果是,則只執行 while 迴圈。

一旦我們將值 False 分配給 flag 變數,直譯器在下一次執行 while 迴圈之前檢查 flag 變數中的值。當看到 flag 變數中的值是 False 時,它終止 while 迴圈。

まとめ

在本文中,我們使用 Python 中的 while 迴圈和條件語句建立了一個帶有無限迴圈的文字選單。我們還討論瞭如何使用 break 語句和 flag 變數終止無限迴圈的執行。

作者: Aditya Raj
Aditya Raj avatar Aditya Raj avatar

Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.

GitHub

相關文章 - Python Loop