Python Forum

Full Version: Menu selection using function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
you can use curses, or for a simpler menu, curses-menu: https://github.com/pmbarrett314/curses-menu
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
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
@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?
(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()
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()
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
@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.