Solved

Python - text menu

Posted on 2012-03-10
7
404 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 28

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 28

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 28

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now