Python Forum
Simple cli menu quits on selection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple cli menu quits on selection
#1
Hello,

First day on Python :)

I was looking to create a simple cli sample menu for my Ubuntu 17.10 and default Python 3.6.3 and i end up to this one:

#!/usr/bin/env python

def menu():
    print ("Welcome, \n 1. Print Hello \n 2. Print World \n 3. Print Python \n 4. Print Hello World")
    choice = input()

    if choice == "1":
        print("Hello")
        menu()

    if choice == "2":
        print("World")
        menu()

    if choice == "3":
        print("Python")
        menu()

    if choice == "4":
        print("Hello World")
        menu()


menu()
When i run it from terminal the menu appear with no issues but when i add a selection it quits to command prompt with no errors :(

Any help?

Thank you
Reply
#2
Works for me, though I did add "choice" to your input, cuz I don't like blank lines.

Output:
Welcome,   1. Print Hello   2. Print World   3. Print Python   4. Print Hello World choice 1 Hello Welcome,   1. Print Hello   2. Print World   3. Print Python   4. Print Hello World choice 2 World Welcome,   1. Print Hello   2. Print World   3. Print Python   4. Print Hello World choice 3 Python Welcome,   1. Print Hello   2. Print World   3. Print Python   4. Print Hello World choice 4 Hello World Welcome,   1. Print Hello   2. Print World   3. Print Python   4. Print Hello World choice 
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Don't know why i have that issue then :(

Quote:userx@ubuntu:~/Desktop$ ./menu.py
Welcome,
1. Print Hello
2. Print World
3. Print Python
4. Print Hello World
1
userx@ubuntu:~/Desktop$

When i select any number it quits....
Reply
#4
(Oct-23-2017, 09:34 PM)pamamolf Wrote: When i select any number it quits....
That's because you run with python 2.
Change shebang #!/usr/bin/env python3
Or drop that and run it with python3 my_script.py

Calling menu() over and over again is not good.
Loop is what's used to avoid this.
Example here is the menu running in a loop,
solve something and always fall back to menu where can repeat or Quit.
def solve_1():
    answer = int(input('What is 2 + 2? '))
    if answer == 4:
        print('Correct')
    input('Push enter to retun to menu')

def solve_2():    
    answer = int(input('What is 10 + 45? '))
    if answer == 55:
        print('Correct')
    input('Push enter to retun to menu')

def menu():
    while True:
        print('(1) Solve something')
        print('(2) Solve something else')
        print('(Q) Quit')
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            solve_1()
        elif choice == '2':
            solve_2()
        elif choice == 'q':
            return
        else:
            print(f'Not a correct choice: <{choice}>,try again')

if __name__ == '__main__':
    menu()
Reply
#5
Great thanks :)
Reply
#6
Only this one doesn't seem to work as i can't trigger it:

print('Not a correct choice: <{choice}>,try again')
I just try to use a number like 7 or even a letter but i didn't got that error :(

It just auto delete the wrong value and waits for a new value....
Reply
#7
You have not copy my code correct,missing a f.
print(f'Not a correct choice: <{choice}>,try again')
f-string is new in 3.6,and should work as you use 3.6.3.
Look like this if run it.
Output:
λ python menu.py (1) Solve something (2) Solve something else (Q) Quit Enter your choice: 7 Not a correct choice: <7>,try again (1) Solve something (2) Solve something else (Q) Quit Enter your choice: hello Not a correct choice: <hello>,try again (1) Solve something (2) Solve something else (Q) Quit Enter your choice: 1 What is 2 + 2? 4 Correct Push enter to retun to menu Not a correct choice: <1>,try again (1) Solve something (2) Solve something else (Q) Quit Enter your choice: q
Reply
#8
Ok i add it back as i was remove it when sublime text report invalid syntax for it....

print(f'Not a correct choice: <{choice}>,try again')
but again it doesn't print the:

Quote:Not a correct choice: <7>,try again

Quote:userx@ubuntu:~/Desktop$ python3 -V
Python 3.6.3
Reply
#9
Save and run it from command line,not from editor:
userx@ubuntu:~/Desktop$ python3 menu.py
if it work there and not in editor,you need to setup editor to use Python 3.6.
Reply
#10
I am using command line and i run it as:

Quote:./menu.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu Choice Selection not Working ChLenx79 5 1,636 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Menu selection using function leodavinci1990 8 14,842 Dec-05-2019, 01:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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