0

我正在创建一个简单的深度菜单系统。到目前为止,一切正常,除非您在比初始菜单系统更深的时候点击“0”(即在主菜单中选择任务 1 或任务 2 之后)。如果您在之后选择它,它会将您发送回显示子任务 1 和返回而不是比任务 1 和任务 2。

我的问题是:我该如何解决这个问题,这是一个高效的菜单系统吗?(即使我需要添加更多的“# 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()
4

2 回答 2

4
于 2012-06-05T22:17:24.803 回答
3

几个指针:

  1. 干燥(不要重复自己)。每次您看到自己重复代码时,就该抽象出并创建一个函数或一个类,您可以从多个点调用它们,但只能在一个点进行调试和编辑。
  2. 尝试找到一个已经可以满足您的需求的软件包,并从头开始编写菜单系统。这样您就可以专注于程序的实质内容。Python 菜单系统似乎可以提供您需要的一切。
于 2012-06-05T21:51:11.693 回答