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
#11
Run it as i show python3 menu.py.
Reply
#12
Ok i did it and it doesn't work :(
Reply
#13
(Oct-24-2017, 12:03 AM)pamamolf Wrote: i add it back as i was remove it when sublime text report invalid syntax for it....

Is your shebang line correctly written to something like: #! /usr/bin/env/python3 and that it is the very first line of the script?
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
#14
(Oct-24-2017, 01:23 PM)pamamolf Wrote: Ok i did it and it doesn't work :(
What dos not work?
I can test it in Linux mint with 3.6.
Will of course work there there as it dos in Windows where i wrote it.
# Make sure that python3 start 3.6
mint@mint ~ $ python3
Python 3.6.2 (default, Aug  6 2017, 12:55:04) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

# The script
mint@mint ~ $ cat menu.py
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()
Output:
# Running the script mint@mint ~ $ python3 menu.py (1) Solve something (2) Solve something else (Q) Quit Enter your choice: 788 Not a correct choice: <788>,try again (1) Solve something (2) Solve something else (Q) Quit Enter your choice: hellooooo Not a correct choice: <hellooooo>,try again (1) Solve something (2) Solve something else (Q) Quit Enter your choice: 2 What is 10 + 45? 55 Correct Push enter to retun to menu (1) Solve something (2) Solve something else (Q) Quit Enter your choice: Q mint@mint ~ $ 
Reply
#15
Works fine for me too.
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
#16
Yes first line is:

#!/usr/bin/python3
and when i start it and add as selection menu number 7 that doesn't exist it doesn't trigger the error as it should be:

Quote:Not a correct choice: <7>,try again
Reply
#17
(Oct-24-2017, 01:49 PM)pamamolf Wrote: Yes first line is:
Do not add anything to my script,just copy it and run it as as described.
Reply
#18
I have also noted from time to time, when pasting code from this site, it gets a bit messed up. Verify what your pasted code looks like is the same as the copied code.
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
#19
I think that the issue is that the above sample code was related to numbers but i am using as a test some commands.....

Can anyone please help to adjust it so the error function actually work?

#!/usr/bin/python3

# Import the modules needed to run the script.
import os
import subprocess

# Menu functions


def solve_1():
    os.system('clear')
    print("Your operating system and version is:\n")
    subprocess.call(["lsb_release", "-d"])
    input('\nPush enter to return to menu:')


def solve_2():
    os.system('clear')
    print("Just a ping for Google using -c 2 parameter:\n")
    subprocess.call(["ping", "-c 2", "www.google.com"])
    input('\nPush enter to return to menu:')


# Main menu

def menu():

    while True:
        os.system('clear')
        print("Welcome,\n")
        print("Please choose the menu you want to start:\n")
        print('(1) Display operating system version')
        print('(2) Ping a web site')
        print('(Q) Quit\n')
        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')

# Main Program


if __name__ == '__main__':


# Launch main menu
    menu()
Reply
#20
O.k., here is what I think is happening. Your "clear" command to clear the screen is preventing you from actually seeing the error, it erases the screen to fast for you to see. Try commenting the "os.system('clear')" and then run your script. 

Output:
Welcome, Please choose the menu you want to start: (1) Display operating system version (2) Ping a web site (Q) Quit Enter your choice: 7 Not a correct choice: <7>,try again Welcome, Please choose the menu you want to start: (1) Display operating system version (2) Ping a web site (Q) Quit Enter your choice:
Either don't clear the screen, or add a time.sleep() after the 'print()'.
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Menu Choice Selection not Working ChLenx79 5 1,567 Nov-23-2022, 05:56 AM
Last Post: deanhystad
  Menu selection using function leodavinci1990 8 14,721 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