'How to exit from a sub menu?

I'm trying to made a option to go out of the program from a sub menu this is my code , but when I use the Exit option in the submenu instead of going out of the program I just go back to the main menu. How can I exit the program from the submenu without using libraries?

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)     


Solution 1:[1]

If you are trying to exit out of the entire script the use sys.exit() like:

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) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laurinchen