Sep-13-2022, 07:21 PM
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:
I would write the same code like this:
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.
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.