Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python - text menu

Posted on 2012-03-10
7
Medium Priority
?
469 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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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. …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses

824 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