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
#1
Yo guys,

I have come quite a bit in coding the crap of this menu but now im kinda stuck! :(
I wonder if there is anyone here that might help me out with a thing or two :P

1, I have now made a "program" for a "client" that helps him handle customers/cars/employes etc.. all this is chooseable with arrow keys!
Exept when I go in a menu program lets say for example the first alternative "handle customers".. I have no clue what so ever how to make arrow keys inside those names.. and how to make "register new customers" open up and give a message like "This option is not available at the moment, contact your administrator for help" or something..
Is it manageble? I think it is since everything is possible while coding hehe :P

2, Also in the code I have written a test that is supposed to be at the top of the program, you can see it in the attached code below, this text does not show when I run the code, how come?


import replit
from getkey import getkey, keys
  
This text does not show in the menubar when I run the program:
  #Text that is supposed to show in menubar:
print("        The bilfirma meny")
print("-._.-._.-._.-._.-._.-._.-._.-._.-._.-\n")
print("Välj något av nedanstående för att gå vidare:")

  
#Defines "choices" for while-true loop so choises made by user is printed on screen
def FirstChoise():
  replit.clear()
  print("Kundlistan:")
  print("***********")
  print("1.Gordon Ramsey\n2.Christiano Ronaldo\n3.Lionel Messi\n4.Neymar  Jr\n5.Zinedin Zidan\n6.Gareth Bale\n7.Kylian Mpabbe\n8.Paul Pogba\n9.David Beckham\n10.Zlatan Ibrahimovic\n")
  print("Registrera nya kunder:\n")
  input("För att gå tillbaka till menyn tryck Enter.")
def SecondChoise():
  replit.clear()
  print("Bilar i lager:")
  print("****************")
  print("1. Volvo XC90\n2. Citroen C3\n3. Audi Q7\n4. Volkswagen Passat\n")
  print("Registrera nya bilar:\n")
  input("För att gå tillbaka till menyn tryck Enter.")
def ThirdChoise():
  replit.clear()
  print("Personal:")
  print("*************")
  print("1. Mikael Persbrant\n2. Stellan Skarsgård\n3. Michael Nyqvist\n4. Peter Stormare\n5. Johan Falk\n")
  print("Registrera ny anställd\n")
  input("För att gå tillbaka till menyn tryck Enter.")
  #To exit the program:
def FourthChoise():
  replit.clear()
  print("Service, verkstad & garantiärenden:")
  print("***********************************")
  print("Service & verkstad:\n1. Nissan Micra: olje & bromsbyte (Ska vara klart till kl 14!)\n2. Seat Almera: Påfyllning av vätskor, kyl/spol/olja. (Ska stå klar till imorgon bitti kl 09.00.) \n3. Tesla modell 3: Batteri byte. (1 vecka har vi på oss att lösa detta!)\n\n\n Garantiärenden:\n 1. Volvo XC90, dåligt jobb med bromsbyte, garantin täcker nytt jobb.")
  input("För att gå tillbaka till menyn tryck Enter.")
def EndProgram():
  replit.clear()
  print("Du har valt att avsluta programmet")
  input("Tryck enter för att avsluta")
  
MenuOptions = ["\t-Hantera Kunder\t", "\t-Hantera bilar\t", "\t-Hantera personal\t","\t-Boka in service, reparationer & garantiärenden\t\n", "\t-Avsluta programmet\t"]

  
#Creates flat number for "default" state
MenuSelected = 0

while(True):
  replit.clear()
  #Clears screen after selection
  print("\x1b[?25l")

  # Prints visual menu, options and highlighting "arrows"
  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] + "<-")    

  #Allows arrowkey "up" to go upward in menu
  keyPressed = getkey()
  if keyPressed == keys.DOWN and MenuSelected + 1 != len(MenuOptions):
    MenuSelected += 1
  #Allows arrowkey "down" to go downward in menu
  elif keyPressed == keys.UP and not (MenuSelected == 0):
    MenuSelected -= 1

  #Allows "Enter key" to select highlighted option and go to that option
  elif keyPressed == keys.ENTER:
    if MenuSelected == 0:
      FirstChoise()
    elif MenuSelected == 1:
      SecondChoise()
    elif MenuSelected == 2:
      ThirdChoise()
    elif MenuSelected == 3:
      FourthChoise()
    elif MenuSelected == 4:
      EndProgram()
      print("\x1b[?25h")
      break
Reply
#2
This is really a new question. It should be in a new thread.

Question 2

You do this at the top of your loop.
  replit.clear()
  #Clears screen after selection
  print("\x1b[?25l")
I don't know what any of this does, but I'm going to trust the comment that it clears the screen. Anything printed prior to this will be erased. That is why you don't see your print from the top of the page. It is erased immediately.

Question 1

You have all this code for doing the first menu:
while(True):
  replit.clear()
  #Clears screen after selection
  print("\x1b[?25l")
 
  # Prints visual menu, options and highlighting "arrows"
  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] + "<-")    
 
  #Allows arrowkey "up" to go upward in menu
  keyPressed = getkey()
  if keyPressed == keys.DOWN and MenuSelected + 1 != len(MenuOptions):
    MenuSelected += 1
  #Allows arrowkey "down" to go downward in menu
  elif keyPressed == keys.UP and not (MenuSelected == 0):
    MenuSelected -= 1
 
  #Allows "Enter key" to select highlighted option and go to that option
  elif keyPressed == keys.ENTER:
    if MenuSelected == 0:
      FirstChoise()
    elif MenuSelected == 1:
      SecondChoise()
    elif MenuSelected == 2:
      ThirdChoise()
    elif MenuSelected == 3:
      FourthChoise()
    elif MenuSelected == 4:
      EndProgram()
      print("\x1b[?25h")
      break
You need to have something similar for every menu.

This is going to be a lot of code unless you can come up with a generic way of menu options to the user, getting their selection, and performing the desired action.
If you want to make menu based programs you might be interested in using GUI tools instead of writing it as a console app.
Reply
#3
(Sep-12-2022, 09:25 PM)deanhystad Wrote: This is really a new question. It should be in a new thread.

Question 2

You do this at the top of your loop.
  replit.clear()
  #Clears screen after selection
  print("\x1b[?25l")
I don't know what any of this does, but I'm going to trust the comment that it clears the screen. Anything printed prior to this will be erased. That is why you don't see your print from the top of the page. It is erased immediately.

Question 1

You have all this code for doing the first menu:
while(True):
  replit.clear()
  #Clears screen after selection
  print("\x1b[?25l")
 
  # Prints visual menu, options and highlighting "arrows"
  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] + "<-")    
 
  #Allows arrowkey "up" to go upward in menu
  keyPressed = getkey()
  if keyPressed == keys.DOWN and MenuSelected + 1 != len(MenuOptions):
    MenuSelected += 1
  #Allows arrowkey "down" to go downward in menu
  elif keyPressed == keys.UP and not (MenuSelected == 0):
    MenuSelected -= 1
 
  #Allows "Enter key" to select highlighted option and go to that option
  elif keyPressed == keys.ENTER:
    if MenuSelected == 0:
      FirstChoise()
    elif MenuSelected == 1:
      SecondChoise()
    elif MenuSelected == 2:
      ThirdChoise()
    elif MenuSelected == 3:
      FourthChoise()
    elif MenuSelected == 4:
      EndProgram()
      print("\x1b[?25h")
      break
You need to have something similar for every menu.

This is going to be a lot of code unless you can come up with a generic way of menu options to the user, getting their selection, and performing the desired action.
If you want to make menu based programs you might be interested in using GUI tools instead of writing it as a console app.

Hi, thanks for the info, appreciate it! I wonder on the meaning ”this is going to be alot of code” is it possible to create this codes in another file and than later on import it to this ”main”’ file? Or am i way over my head now 😂
Reply
#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
#5
(Sep-13-2022, 07:21 PM)deanhystad Wrote: 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.

Once again thanks for the input!
I am very new to the coding game, currently in a school course and we are learning ”algorythem” and ”datatructure” at the moment so everything you guys say its all new to me, learning as much as i can! 😎
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,663 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 6,868 Dec-02-2019, 04:41 PM
Last Post: Agile741
  [split] FileNotFoundError...System cannot find the path specified powerrocker 1 2,062 Oct-03-2019, 09:09 AM
Last Post: buran
  [split] Lottery System on my mobile website sibt007 1 2,458 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