如何从子菜单中退出

  • 本文关键字:退出 菜单 python
  • 更新时间 :
  • 英文 :


我试图从子菜单中选择退出程序,这是我的代码,但当我使用子菜单中的退出选项而不是退出程序时,我只返回主菜单。如何在不使用库的情况下从子菜单中退出程序?

def main_menu(input_expected):
while input_expected != "X":
if input_expected=="1":
sub_menu()
else:
print("wrong choice")

print("*** Main Menu ***")
print("----------------------")
input_expected = input(" 
n [1] Start 
n [X] Exit 
n Write your choice: ")
def sub_menu():
new_input = input(" 
n [1] Continue 
n [2] Go back 
n [X] Exit 
n Write your choice: ")
if new_input =="X":     
input_expected = new_input
main_menu(input_expected) 
print("*** Main Menu ***")
print("----------------------")
input_expected = input(" 
n [1] Start 
n [X] Exit 
n Write your choice: ")
main_menu(input_expected)     

如果您试图退出整个脚本,请使用sys.exit()类似:

from sys import exit
def main_menu(input_expected):
while input_expected != "X":
if input_expected=="1":
sub_menu()
else:
print("wrong choice")

print("*** Main Menu ***")
print("----------------------")
input_expected = input(" 
n [1] Start 
n [X] Exit 
n Write your choice: ")


def sub_menu():
new_input = input(" 
n [1] Continue 
n [2] Go back 
n [X] Exit 
n Write your choice: ")
if new_input =="X":     
exit() 
print("*** Main Menu ***")
print("----------------------")
input_expected = input(" 
n [1] Start 
n [X] Exit 
n Write your choice: ")
main_menu(input_expected) 

最新更新