Solved

Python - text menu

Posted on 2012-03-10
7
437 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 500 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Need help in achieving fail-over / HA in Ubuntu Server. 6 54
What regex will remove duplicate rel="nofolow" tags? 3 133
Python 3.5.2 - Script Help 3 125
phython 2 64
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
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 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…
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…

751 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