Steven
Steven

Reputation: 75

Is this efficient and where am I going wrong? (Python Menu System)

I'm creating a simple deep menu system. So far everything works fine unless you hit '0' while deeper than the initial menu system (i.e. after selecting task 1 or task 2 in the main menu.) If you select it after, it sends you back to show Subtask 1 and Back rather than task 1 and task 2.

My question is: How do I fix this and is this an efficient menu system? (even if I need to add more '# Comment' lines to explain it.)

# Multitasker - Deep Menu System
# A Menu that Allows the User to Select Tasks, Subtasks and Deeper Subtasks

# This is the initial screen.
def homescreen():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X             READY TO START?            X
            X          ---------------------         X
            X            PRESS THE 'ENTER'           X
            X              KEY TO BEGIN!             X
            X          ---------------------         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
         """,end=" ")

def task1():
    task1 = None
    while task1 != "0":
        print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXX TASK OPTIONS XXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X   ----------------------------------   X
            X   1 - SUBTASK 1                        X
            X   2 - BACK                             X
            X   ----------------------------------   X
            X   0 - Quit                             X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

        task1 = input("\n\t\tPick a Choice Between 0-2:\t#")
        print()

# Exit
        if task1 == ('0'):
            homescreen()
            input(" ")
            menu = None

        elif task1 == ('1'):
            subtask1()
        elif task1 == ('2'):
            return
        else:
            notatask()

def subtask1():
    subtask1 = None
    while subtask1 != "0":
        print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXX TASK OPTIONS XXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X   ----------------------------------   X
            X   1 - DEEP SUBTASK 1                   X
            X   2 - BACK                             X
            X   ----------------------------------   X
            X   0 - Quit                             X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

        subtask1 = input("\n\t\tPick a Choice Between 0-2:\t#")
        print()

# Exit
        if subtask1 == ('0'):
            homescreen()
            input(" ")
            menu = None

        elif subtask1 == ('1'):
            deepsubtask1()

        elif subtask1 == ('2'):
            return
        else:
            notatask()
def deepsubtask1():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X         THIS IS DEEP SUBTASK 1         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                     PRESS 'ENTER' TO RETURN!
            """, end=" ")
    input(" ")

# TASK 2 

def task2():
    task2 = None
    while task2 != "0":
        print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXX TASK OPTIONS XXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X   ----------------------------------   X
            X   1 - SUBTASK 2                        X
            X   2 - BACK                             X
            X   ----------------------------------   X
            X   0 - Quit                             X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

        task2 = input("\n\t\tPick a Choice Between 0-2:\t#")
        print()

# Exit
        if task2 == ('0'):
            homescreen()
            input(" ")
            menu = None

        elif task2 == ('1'):
            subtask2()
        elif task2 == ('2'):
            return
        else:
            notatask()

def subtask2():
    subtask2 = None
    while subtask2 != "0":
        print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXX TASK OPTIONS XXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X   ----------------------------------   X
            X   1 - DEEP SUBTASK 2                   X
            X   2 - BACK                             X
            X   ----------------------------------   X
            X   0 - Quit                             X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

        subtask2 = input("\n\t\tPick a Choice Between 0-2:\t#")
        print()

# Exit
        if subtask2 == ('0'):
            homescreen()
            input(" ")
            menu = None

        elif subtask2 == ('1'):
            deepsubtask2()
        elif subtask2 == ('2'):
            return
        else:
            notatask()
def deepsubtask2():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X         THIS IS DEEP SUBTASK 2         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                     PRESS 'ENTER' TO RETURN!
            """, end=" ")
    input(" ")

# Errors

def notatask():
    print("", end=" ")
def final():
    print("No More Tasks!")

# Makes choice equal to no actual selection or choice
homescreen()
menu = None
input(" ")
# Start the Main Menu
while menu != "0":
    print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXX TASK OPTIONS XXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X   ----------------------------------   X
            X   1 - TASK 1                           X
            X   2 - TASK 2                           X
            X   ----------------------------------   X
            X   0 - Quit                             X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

    menu = input("\n\t\tPick a Choice Between 0-2:\t#")
    print()

    # Exit
    if menu == ('0'):
        print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X     MULTITASKER - DEEP MENU SYSTEM     X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X              READY TO START?           X
            X           ---------------------        X
            X             PRESS THE 'ENTER'          X
            X               KEY TO BEGIN!            X
            X           ---------------------        X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        """,end=" ")
        input("                         ")
        menu()

    elif menu == ('1'):
        task1()
    elif menu == ('2'):
        task2()
    else:
        notatask()

Upvotes: 0

Views: 725

Answers (2)

poke
poke

Reputation: 387983

As gauden said, you should really abstract your interface a lot. Write some utility functions that generate all those borders and boxes for you, so that you only need to put in actual content. Separate your logic from the presentation and abstract further, so you might even be able to switch out the console output to a windowed output at some point, without having to change the code.

For example I created a simple set of functions to create generic boxes and screens. It’s not very abstract yet, but it should show you a first possible step. It also makes use of some more useful language features, for example the multiplication of strings by scalars (e.g. 'X' * 42 equals to one of your XXX… lines) or the automated string padding when using string formatting.

The example below creates your homescreen, but the actual creation of that are two lines (actually the title output could be extracted out too), with the rest being reusable functions for the other screens:

def printLine (text = ''):
    print('X {: ^38} X'.format(text))

def printBorder (title = None):
    print('X' * 42)

def printBox (*lines, title = None, showBottomBorder = False):
    printBorder(title)
    printLine()
    for line in lines:
        printLine(line)
    printLine()
    if showBottomBorder:
        printBorder()

printBox('MULTITASKER - DEEP MENU SYSTEM')
printBox('READY TO START?', '-' * 21, "PRESS THE 'ENTER'", 'KEY TO BEGIN!', '-' * 21, showBottomBorder = True)

Upvotes: 4

daedalus
daedalus

Reputation: 10923

A couple of pointers:

  1. DRY (Don't repeat yourself). Every time you see yourself repeating code, that is the time to abstract out and create a function or a class that you can call on from multiple points but only debug and edit at one point.
  2. Try to find a package that already does what you want and take the pain out of writing a menu system from scratch. That way you can focus on the substance of your program. Python Menu System appears to deliver everything you need here.

Upvotes: 3

Related Questions