Python Forum
Menu selection using function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Menu selection using function
#1
Hi I am trying to print out a string that shows the menu item selected in a function. I have the following code. But it is seems I don't have the logic correctly. Any suggestions?
def showMenu():
    print ("\nExample menu")
    print ("-----------------")
    print ("1) File deletion")
    print ("2) File creation")
    print ("3) Exit\n")
    data=int(input("Enter your choice: "))
    return data
data = showMenu()
while data!=3:
    user=showMenu()
    if data==1:
        print("Files deleted")
    elif datat==2:
        print("Files created")
    elif user==3:
        print("Exiting")
    else:
        print("'%s' is an unknown option."%user)
Reply
#2
you can use curses, or for a simpler menu, curses-menu: https://github.com/pmbarrett314/curses-menu
Reply
#3
Your function returns string and in the while loop you are comparing data to int. So it's infinite loop and still ifs inside are False and it will print always the else part
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#5
@snippsat Thanks for that. So this new string formatting technique is adopted fully. Is this a new PEP?
And what is it that lines 29 and 30 do exactly?
Reply
#6
(Nov-19-2019, 11:21 PM)leodavinci1990 Wrote: @snippsat Thanks for that. So this new string formatting technique is adopted fully. Is this a new PEP?
It start to be very common to use now,i have used for 2-3 years now since 3.6 come out.
PEP 498 and f-Strings guide

(Nov-19-2019, 11:21 PM)leodavinci1990 Wrote: And what is it that lines 29 and 30 do exactly?
If i import the file let call it my_menu.py,it want run at import time.
Usually don't want stuff to run when import it.

With if __name__ == '__main__':
E:\div_code
λ ptpython
>>> import my_menu

>>> my_menu.menu() # have to call menu to runt it.

Example menu
-----------------
1) File deletion
2) File creation
Q) Exit

Enter your choice: q

>>> exit()
Only menu():
E:\div_code
λ ptpython
>>> import my_menu # It start running code

Example menu
-----------------
1) File deletion
2) File creation
Q) Exit

Enter your choice: q

>>> exit()
Reply
#7
def mainMenu():
print("\nExample Menu")
print("----------------")
print("1) File deletion")
print("2) File creation")
print("3) Exit\n")
selection=int(input("Enter your choice: "))
if selection==1:
print("Files are deleted")
elif selection==2:
print("Files are created")
elif selection==3:
print("Exiting")
else:
print("Invalid Choice")
mainMenu()
Reply
#8
Photo 
def mainMenu():
    print("\nExample Menu")
    print("----------------")
    print("1) File deletion")
    print("2) File creation")
    print("3) Exit\n")
    selection=int(input("Enter your choice: "))
    if selection==1:
        print("Files are deleted")
    elif selection==2:
        print("Files are created")
    elif selection==3:
        print("Exiting")
    else:
        print("Invalid Choice")
mainMenu()
Output:
================= Example Menu ---------------- 1) File deletion 2) File creation 3) Exit Enter your choice: 2 Files are created Example Menu ---------------- 1) File deletion 2) File creation 3) Exit Enter your choice: 1 Files are deleted Example Menu ---------------- 1) File deletion 2) File creation 3) Exit Enter your choice: 3 Exiting
Reply
#9
@sumana putting all code in one function dos work,but it's not the best way at all.
When reading or testing code,so do it make much more sense to have file deletion/creation code isolated from menu function.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu Choice Selection not Working ChLenx79 5 1,548 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,381 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Problem with user defined main menu function stefzeer 3 2,406 Mar-27-2020, 06:12 AM
Last Post: buran
  Simple cli menu quits on selection pamamolf 19 9,599 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