Python Forum
[split] Please help with menu system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Please help with menu system
#4
Managing the volume of code is not your problem. Your problem is that you are going to write a lot of lines of code when you should be writing few lines of code. This has to do with how you currently think about code.

Here's a small example. Your program includes this code:
  if MenuSelected == 0:
    print("->" + MenuOptions[0] + "<-")
    print(MenuOptions[1])
    print(MenuOptions[2])
    print(MenuOptions[3])
    print(MenuOptions[4])
  
  elif MenuSelected == 1:
    print(MenuOptions[0])
    print("->" + MenuOptions[1] + "<-")
    print(MenuOptions[2])
    print(MenuOptions[3]) 
    print(MenuOptions[4])
  
  elif MenuSelected == 2:
    print(MenuOptions[0])
    print(MenuOptions[1])
    print("->" + MenuOptions[2] + "<-")
    print(MenuOptions[3])  
    print(MenuOptions[4])
  
  elif MenuSelected == 3:
    print(MenuOptions[0])
    print(MenuOptions[1])
    print(MenuOptions[2])
    print("->" + MenuOptions[3] + "<-") 
    print(MenuOptions[4])
  
  elif MenuSelected == 4:
    print(MenuOptions[0])
    print(MenuOptions[1])
    print(MenuOptions[2])
    print(MenuOptions[3])
    print("->" + MenuOptions[4] + "<-") 
Notice all the repetition? This code repeats the same 6 lines of code over and over with only minor variation. The code is also directly tied to a particular menu list and selection variable. If you wanted to add 5 more menus, you would probably cut and paste this code 5 times and make small modifications.

I would write the same code like this:
def print_menu(menu, selection):
    """Print menu option with highlighting arrow"""
    for index, option in enumerate(menu):
        prefix = "->" if index == selection else "  "
        print(f"{prefix}{option}")

print_menu(MenuOptions, MenuSelection)
If I wanted to add 5 more menus I would define 5 more menu options lists (5 lines of code).

Just something you should start thinking about. Cut and paste is not a programming tool. Start thinking about how you can use functions and data structures to make your single use code more generic.
dvejsa likes this post
Reply


Messages In This Thread
[split] Please help with menu system - by dvejsa - Sep-12-2022, 07:47 PM
RE: [split] Please help with menu system - by deanhystad - Sep-13-2022, 07:21 PM
RE: Newbie needs help part 2 - by deanhystad - Sep-12-2022, 09:25 PM
RE: Newbie needs help part 2 - by dvejsa - Sep-13-2022, 06:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,851 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 7,049 Dec-02-2019, 04:41 PM
Last Post: Agile741
  [split] FileNotFoundError...System cannot find the path specified powerrocker 1 2,116 Oct-03-2019, 09:09 AM
Last Post: buran
  [split] Lottery System on my mobile website sibt007 1 2,504 Sep-27-2018, 12:56 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020