简体   繁体   English

这有效吗,我在哪里出错? (Python菜单系统)

[英]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 (ie 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. 到目前为止,一切正常,除非您在比初始菜单系统更深的位置击中“ 0”(即在主菜单中选择了任务1或任务2之后)。如果在此之后选择它,它将使您返回显示子任务1,然后返回而不是任务1和任务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.) (即使我需要添加更多的“#Comment”行来进行说明。)

# 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()

As gauden said, you should really abstract your interface a lot. 正如gauden所说,您确实应该对接口进行很多抽象。 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 (eg 'X' * 42 equals to one of your XXX… lines) or the automated string padding when using string formatting. 它还利用了一些更有用的语言功能,例如,字符串乘以标量(例如, 'X' * 42等于您的XXX…行之一)或使用字符串格式化时的自动字符串填充。

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)

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. Python Menu System似乎可以在这里提供您所需的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM