4 Replies - 335 Views - Last Post: 24 October 2017 - 06:21 AM Rate Topic: -----

#1 pamamolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 17

Simple cli menu quits on selection

Posted 23 October 2017 - 12:11 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Simple cli menu quits on selection

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Simple cli menu quits on selection

Posted 23 October 2017 - 04:03 PM

Well first of all you don't need to call menu() in each if. You can call it one time at the end of the function. I also ran your script in your standard run of the mill windows command prompt and it simply shows the menu, accepts the selection, prints the message and prints the menu again in a continuous loop (as it appears you have designed it to do). So I don't see any problems. I don't think it should be closing the window. If this was C++ I would expect it to close the window when it completes the program (unless you state for it to do otherwise).

Have you tried running it in IDLE or something?

P.S. Here is the code...

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")
 
    if choice == "2":
        print("World")
 
    if choice == "3":
        print("Python")
 
    if choice == "4":
        print("Hello World")
    
    menu() # Notice one call to menu here.
 
 
menu()


Was This Post Helpful? 0
  • +
  • -

#3 pamamolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 17

Re: Simple cli menu quits on selection

Posted 23 October 2017 - 04:54 PM

Great thank you :)

Any ideas where i should add a \n at this line?

subprocess.call(["lsb_release", "-d"])

Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 320
  • View blog
  • Posts: 1,055
  • Joined: 27-December 13

Re: Simple cli menu quits on selection

Posted 24 October 2017 - 02:25 AM

Further to the code by Martyr2 in post #2:

The call to 'menu()' from inside the same function is recursive.
Recurciveness is fine when needed but often bad if not needed. Here it is not.

Better is to place the call to 'menu()' in a loop in the main part of the code:
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")
 
    elif choice == "2":
        print("World")
 
    elif choice == "3":
        print("Python")
 
    elif choice == "4":
        print("Hello World")

    else:
        print("Incorrect input")


while True: # never ending loop
    menu()


I have also converted the many 'if' to 'elif' and a final 'else:'.
'elif' is more efficient as subsequent comparison(s) is(are) skipped as soon as one true outcome is found; e.g. if choice == "1", only line 6 executes and all the 'elif' are ignored.
The 'else:' will give a feedback upon unexpected input.

This post has been edited by DK3250: 24 October 2017 - 02:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 pamamolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 17

Re: Simple cli menu quits on selection

Posted 24 October 2017 - 06:21 AM

Great thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1