python循环怎么结束_Python:如何在循环中结束程序?

我制作的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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值