Python Forum
Menu selection using function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Menu selection using function
#4
This is how would set it up,and do not use %s string formatting anymore.
All is in function will always fall back to menu function that run the main loop,here can do new task or Quit out.
def file_deletion():
    file_to_delete = input('File to delete: ')
    print(f'Deleting <{file_to_delete}>\n')
    input('Push enter to retun to menu')

def file_creation():
    pass

def show_menu():
    print ("\nExample menu")
    print ("-----------------")
    print ("1) File deletion")
    print ("2) File creation")
    print ("Q) Exit\n")

def menu():
    while True:
        show_menu()
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            file_deletion()
        elif choice == '2':
            file_creation()
        elif choice == 'q':
            return
        else:
            print(f'Not a correct choice: <{choice}>,try again')

if __name__ == '__main__':
    menu()
Output:
E:\div_code λ python file_menu.py Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: 999 Not a correct choice: <999>,try again Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: 1 File to delete: hello.txt Deleting <hello.txt> Push enter to retun to menu Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: q E:\div_code
Reply


Messages In This Thread
Menu selection using function - by leodavinci1990 - Nov-09-2019, 10:54 AM
RE: Menu selection using function - by Larz60+ - Nov-09-2019, 11:13 AM
RE: Menu selection using function - by buran - Nov-09-2019, 11:55 AM
RE: Menu selection using function - by snippsat - Nov-09-2019, 12:59 PM
RE: Menu selection using function - by snippsat - Nov-20-2019, 05:56 AM
RE: Menu selection using function - by sumana - Nov-21-2019, 04:15 PM
RE: Menu selection using function - by sumana - Dec-05-2019, 05:54 AM
RE: Menu selection using function - by snippsat - Dec-05-2019, 01:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu Choice Selection not Working ChLenx79 5 1,665 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,597 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Problem with user defined main menu function stefzeer 3 2,472 Mar-27-2020, 06:12 AM
Last Post: buran
  Simple cli menu quits on selection pamamolf 19 9,832 Oct-26-2017, 02:09 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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