[Python] How to efficiently make a text-based menu?

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently making a program with a text based menu, and I was wondering if there was anyway to avoid an excessive amount of if-statements while keeping it object oriented. I thought of something like this:

    class Options:
    def displayOptions():
    #display user options
    def option1():
    #do option 1
    def option2():
    #do option 2
    def option3():
    #do option 3 and so on

    class Menu(Options):
    options = {1: option1(), 2: option2(), 3: option3()} #and so on
    def promptUser():
    #prompts user to make a choice
    def printHeader():
    #prints header
    def handleInput():
    #checks if input is valid
    def begin():
    #initialize menu


    Would something like this be convoluted? Is the Options class pointless? I'm pretty new to OOP and I was wondering how this would most efficiently be done.

    Continue reading...

Compartilhe esta Página