Avatar of ezzadin
ezzadin
 asked on

Python - text menu

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.
Python

Avatar of undefined
Last Comment
pepr

8/22/2022 - Mon
pepr

Post your code here, please, to discuss what you did and what can be done.
ezzadin

ASKER
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.
ASKER CERTIFIED SOLUTION
HonorGod

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ezzadin

ASKER
Thanks.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
HonorGod

You are very welcome.

Thanks for the grade & the points.

Good luck & have a great day.
pepr

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 ;)
pepr

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.