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


i'm creating simple deep menu system. far works fine unless hit '0' while deeper initial menu system (i.e. after selecting task 1 or task 2 in main menu.) if select after, sends show subtask 1 , rather task 1 , task 2.

my question is: how fix , efficient menu system? (even if need add more '# comment' lines explain it.)

# multitasker - deep menu system # menu allows user select tasks, subtasks , deeper subtasks  # initial screen. def homescreen():     print("""             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             x                                        x             x     multitasker - deep menu system     x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             x                                        x             x             ready start?            x             x          ---------------------         x             x            press 'enter'           x             x              key 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 -                             x             x   ----------------------------------   x             x   0 - quit                             x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             """, end=" ")          task1 = input("\n\t\tpick 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 -                             x             x   ----------------------------------   x             x   0 - quit                             x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             """, end=" ")          subtask1 = input("\n\t\tpick 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         deep subtask 1         x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                       press 'enter' 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 -                             x             x   ----------------------------------   x             x   0 - quit                             x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             """, end=" ")          task2 = input("\n\t\tpick 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 -                             x             x   ----------------------------------   x             x   0 - quit                             x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx             """, end=" ")          subtask2 = input("\n\t\tpick 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         deep subtask 2         x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                       press 'enter' return!             """, end=" ")     input(" ")  # errors  def notatask():     print("", end=" ") def final():     print("no more tasks!")  # makes choice equal no actual selection or choice homescreen() menu = none input(" ") # start 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 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 start?           x             x           ---------------------        x             x             press 'enter'          x             x               key begin!            x             x           ---------------------        x             x                                        x             xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         """,end=" ")         input("                         ")         menu()      elif menu == ('1'):         task1()     elif menu == ('2'):         task2()     else:         notatask() 

as gauden said, should abstract interface lot. write utility functions generate borders , boxes you, need put in actual content. separate logic presentation , abstract further, might able switch out console output windowed output @ point, without having change code.

for example created simple set of functions create generic boxes , screens. it’s not abstract yet, should show first possible step. makes use of more useful language features, example multiplication of strings scalars (e.g. 'x' * 42 equals 1 of xxx… lines) or automated string padding when using string formatting.

the example below creates homescreen, actual creation of 2 lines (actually title output extracted out too), rest being reusable functions 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()     line in lines:         printline(line)     printline()     if showbottomborder:         printborder()  printbox('multitasker - deep menu system') printbox('ready start?', '-' * 21, "press 'enter'", 'key begin!', '-' * 21, showbottomborder = true) 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -