Python:如何在循环中结束程序?

标签 python while-loop break

<分区>

我制作的 Python 程序是基于菜单的,需要用户输入才能在程序中导航。

我放置了一个 while 循环,以便将用户带回到开始菜单以执行任务,但是“按 0 退出”选项之一只会重新启动循环而不是结束程序。下面是代码:

terms = {"ALU":"Arithmetic Logic Unit", "CPU":"Central Processing Unit", "GPU":"Graphics Processing Unit"}
while True:
    print(
    """
    Computing Terminology

    0 - Quit
    1 - Look Up a Term
    2 - Add a Term
    3 - Redefine  a Term
    4 - Delete a Term
    5 - Display All Terms
    """
    )
    menu = input("Choice: ")
    print()
    while menu != "0":
       if menu == "1":
            print("\n")
            term = input("Type in a term you wish to see: ")
            if term in terms:
                definition = terms[term]
                print("\n")
                print(term, "means", definition, "\n")
                break
            else:
                print("This term does not exist.\n")
                break
        elif menu == "2":
            term = input("What term would you like to add?: ")
            if term not in terms:
                print("\n")
                definition = input("What's the definition?: ")
                terms[term] = definition
                print("\n")
                print(term, "has been added.\n")
                break
            else:
                print("\n")
                print("Term already exists, try redefining it instead.\n")
                break
        elif menu == "3":
            term = input("Which term do you want to redefine?: ")
            if term in terms:
                definition = input("What's the new definition?: ")
                terms[term] = definition
                print("\n")
                print(term, "has been redefined.\n")
                break
            else:
                print("\n")
                print("That term doesn't exist, try adding it instead.\n")
                break
        elif menu == "4":
            term = input("Which term would you like to delete?: ")
            if term in terms:
                del terms[term]
                print("\n")
                print("The term has been deleted.\n")
                break
            else:
                print("\n")
                print("This term doesn't exist.\n")
                break
        elif menu == "5":
            print("\n")
            print("Terms available are: ")
            for term in terms:
                print("\n", term, "\n")
        else:
            print("\n")
            print("Sorry, but", menu, "is not a valid choice.\n")
            break

print("\n")
input("Press any key to exit.") #if user enters 0 I want the program to end here.

最佳答案

这是我一直以来的做法

def print_menu():
    print('What would you like to do: ')
    print('1. Retrieve data')
    print('2. Store Data')
    print('3. Exit')

while True:
    print_menu()
    choice = int(input('Enter a number: '))
    if choice == 1:
        #blah
    elif choice == 2:
        #blah blah
    elif choice == 3:
        break

关于Python:如何在循环中结束程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20953308/

相关文章:

python - 光标删除字符——Spyder程序

c++ - "} while (0);"总是等于 "break;} while (1);"吗?

Python 的构造 - .sizeof() for construct depending on its parent

python - 关于opencv的翘曲没有给出准确的结果

C 反向函数不适用于 32 个字符

python - 如何让 while 循环识别它是文件结尾

python - 父类方法调用父类方法,而不是子类方法

python - RQ - 清空和删除队列

javascript - while 循环内的 switch 语句

java - while循环无法停止