0

Hi I'm new to Python.

I can't seem to go back to a submenu. Using continue or break will make it go to the MAIN menu. Will using def help? If so, how is def used in that situation?

2
Contributors
1
Reply
15
Views
2 Years
Discussion Span
Last Post by Lucaci Andrew
0

Always read the documentation if in doubt:
PyDoc - def

As for you, here's an example of a submenu (Python 3.x):

def firstMenu():
    print ('''This is the first menu
1. Hello
2. World
3. Submenu
q - exit
    ''')
    while True:
        answer = input("Insert command: ")
        if answer == "1": print("Hello")
        elif answer == "2": print("World")
        elif answer == "3": 
            secondMenu()
            print("Welcome back.")
        elif answer == "q": 
            print("Leaving program.")
            break
        else: print("Invalid command.")


def secondMenu():
    print('''This is the second menu
1. Say second
2. Say menu
q - return to previous menu.
    ''')
    while True:
        answer = input("Insert command: ")
        if answer == "1": print("Say: second.")
        elif answer == "2": print("Say: menu")
        elif answer == "q": break
        else: print("Invalid command.")

firstMenu()

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.