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