Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python - text menu

Posted on 2012-03-10
7
Medium Priority
?
451 Views
Last Modified: 2012-03-14
Hi,

I have create a text menu with Python. Once an option is selected, a simple task is performed. Once the task is done, the user is back to the menu (loop). I have created a function and then calling that function from within my codes to display the menu.  

How can I not display the entire menu again (once user is done with first selected option) ?

so something like this:

Please select 1 or 2?
1- Milk
2- Egg


user selects 1

Milk is good sources of calcium.
select another option:


I have used loop to create the program, but I'm not sure how to not display the entire menu again.

Thanks.
0
Comment
Question by:ezzadin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 37706770
Post your code here, please, to discuss what you did and what can be done.
0
 

Author Comment

by:ezzadin
ID: 37716094
Hi,

Thanks. I somehow managed to fix it by using a function. however, I got another issue now:

I'm trying to print a message and this is what I did:

def msg2(n):
    a = "You selected"
    if n == 1:
        return (a, "ITEM 1")
    if n == 2:
        return (a, "ITEM 2")

Open in new window


and when I do

print msg2(1)

it prints:

('You selected', 'ITEM 1')

Why the ( and ' and , are being printed. I want the output to be:

You selected ITEM 1.

Thanks.
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 37716368
Something like this perhaps?

def msg2(n):
    a = "You selected "
    if n == 1 :
        a += "ITEM 1"
    elif n == 2 :
        a += "ITEM 2"
    else :
        a += "an unrecognized value: %d" % n
    return a

Open in new window


Which appends the appropriate text to the local variable (a), and returns the updated value
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:ezzadin
ID: 37716444
Thanks.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 37716516
You are very welcome.

Thanks for the grade & the points.

Good luck & have a great day.
0
 
LVL 29

Expert Comment

by:pepr
ID: 37718675
For the return (a, "ITEM 1").  When you put the two or more items to parenthesis, you form a tuple (object of one of the basic types [the tuple type]).  

When you pass any object to the print command, it calls its method that returns a string representation of the object.  The method should return "a human readable string" -- which is a value of the string if it is a string object.  If the object type is not of a string type, its .__str__() method returns some human readable string representation.  If there is no explicit definition of the method, the .__repr__() method is called.  It returns "a technical representation of the value of the object".  If possible, the technical representation of an object is the string that--if copy/pasted to the source code--causes creation of the object with the same value.  This is also the story behind your (a, "ITEM 1"). Because of that the print printed what it printed ;)
0
 
LVL 29

Expert Comment

by:pepr
ID: 37718823
You can also try to generalize the menu -- see below:

a.py
def selectItemFrom(menu):
    '''Returns identification of the selected item.'''

    # Build the prompt.
    prompt = 'Please, type 1-' + str(len(menu)) + ': '

    # Display the menu once.
    print
    for n, txt in enumerate(menu):
        print '{0} - {1}'.format(n + 1, txt)  # zero-based to one-based
    print

    selected = 0
    while selected < 1 or selected > len(menu):

        # Ask the user and get his/her input.
        selected = raw_input(prompt)

        # Convert the typed-in string to the integer value.
        try:
            selected = int(selected)
        except:
            selected = 0

    return menu[selected - 1]   # one-based to zero-based


if __name__ == '__main__':

    print 'What do you prefer?'
    menu = ('milk', 'egg')     # Here a tuple but it can also be a list.
    answer = selectItemFrom(menu)
    print 'OK, you prefer', answer

    print '\nWhat type of pasta do you like?'
    answer = selectItemFrom(('spaghetti', 'spaghettini', 'macaroni', 'farfale'))
    print 'OK, you prefer', answer

Open in new window


It prints on my console:
c:\tmp\___python\ezzadin>python a.py
What do you prefer?

1 - milk
2 - egg

Please, type 1-2: help
Please, type 1-2: xxx
Please, type 1-2: 5
Please, type 1-2: 2
OK, you prefer egg

What type of pasta do you like?

1 - spaghetti
2 - spaghettini
3 - macaroni
4 - farfale

Please, type 1-4: 0
Please, type 1-4: 5
Please, type 1-4: 3
OK, you prefer macaroni

Open in new window

0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question