Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:08:23+00:00 2026-06-04T16:08:23+00:00

I’m learning Python (and programming in general) by making small programs. Below is a

  • 0

I’m learning Python (and programming in general) by making small programs. Below is a basic shopping program which will return a list of items to buy based on the selected food.

I’d like to improve it and allow user to select several foods at once (e.g. user input would be “1, 2, 3”) and return a list of ingredients based on that.

What kind of approach should I take? I’m using Python 2.7 and here is my existing code:

mm_soup = ['minced meat', 'potatoes', 'frozen vegetable']
sunday_soup = ['chicken with bones', 'noodles', 'soup vegetable']
gulas = ['pork meat', 'food cream', 'potatoes', 'onion', 'frozen peas']

print "What would you like to cook on weekend?"
print "Here are the options:"
print "1. Minced Meat Soup"
print "2. Sunday Soup"
print "3. Gulas"

choose = raw_input("> ")

if choose == '1':
    print "Buy", ", ".join(mm_soup) + "." 

elif choose == '2':
    print "Buy", ", ".join(sunday_soup) + "."

elif choose == '3':
    print "Buy", ", ".join(gulas) + "."

else:
    print "Hmmm. No such food on the list."
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:08:24+00:00Added an answer on June 4, 2026 at 4:08 pm

    There are some common problems with your code, so let’s begin by fixing those.

    You have multiple items you want to present at the user, and you are hard-coding those values. This makes a lot of effort for you as you have to repeat yourself a lot. Look at your choice lines, they all come to basically the same thing. You also repeat yourself by defining what the numbers link to in your description, and in your code. Let’s try to simplify this with a data structure.

    Here, we make a list of all the options – a list of tuples, defining the name and set of items for the given food. We use a set here as we don’t need to order the items.

    options = [
        ("Minced Meat Soup", {'minced meat', 'potatoes', 'frozen vegetable'}),
        ("Sunday Soup", {'chicken with bones', 'noodles', 'soup vegetable'}),
        ("Gulas", {'pork meat', 'food cream', 'potatoes', 'onion', 'frozen peas'}),
    ]
    

    This gives us a good data structure to begin with.

    We can then make our question, rather than manually constructing it, we can construct it from our list of options using a loop:

    print "What would you like to cook on weekend?"
    print "Here are the options:"
    for option, (name, values) in enumerate(options, 1):
        print str(option)+". "+name
    

    Note the use of the enumerate() builtin in order to give us the numbers for the options. As you want to start from 1, and Python counts from 0 normally, we pass that in too.

    This gives us our output, but we can now easily add more items without modifying the existing code. We ask like before, and then instead of a load of if/elifs, we can simply get the index they give us from the list. We first have to change the string to a number, and then take away one (as Python counts from 0). This gives us:

    _, values = options[int(choose)-1]
    

    (Using tuple unpacking to ignore the first value, as it’s the name, which we don’t need).

    The only issue now is what will happen if the user types in a number out of range or an word instead, for example. You could check it before you convert to an int and use it, but it’s Pythonic to simply try it, and catch the thrown exceptions upon failure. For example:

    try:
        _, values = options[int(choose)-1]
        print "Buy", ", ".join(values) + "."
    except (IndexError, ValueError):
        print "Hmmm. No such food on the list."
    

    This makes the entire program much smaller, and also note how easy it is to add new items, you simply add them to the list.

    So how do we deal with multiple items? Well, that’s pretty simple now too. We can take the user’s input, split in on commas, and strip the values to remove any spaces, then do the same thing we did before:

    for choice in choose.split(","):
        choice = choice.strip()
    
        try:
            _, values = options[int(choice)-1]
            print "Buy", ", ".join(values) + "."
        except (IndexError, ValueError):
            print "Hmmm. No such food on the list."
    

    This works, printing out multiple buy lines, but it’s not optimal, a better idea would be to produce one bigger shopping list containing all of the needed items.

    We can build this by building up a set of all of the items as we loop, then printing out that set.

    shopping_list = []
    for choice in choose.split(","):
        choice = choice.strip()
        try:
            _, values = options[int(choice)-1]
            shopping_list.append(values)
        except (IndexError, ValueError):
            print "Hmmm. No such food on the list."
    

    This is a little inefficient and ugly, however. Python has some built in functionality to build up lists – list comprehensions. We can do this operation like so:

    try:
        shopping_list = [options[int(choice.strip())-1][3] for choice in choose.split(",")]
    except (IndexError, ValueError):
        print "Hmmm. No such food on the list."
    

    Now we need to print out all the values in the list. Remember that this is a list of sets, so ", ".join() won’t do exactly what we want. We have two options here. We can either use a generator expression to join the sets first, then join the joined strings:

    print "Buy " + ", ".join(", ".join(values) for values in shopping_list) + "."
    

    Or, we could use itertools.chain.from_iterable() to return a flattened iterator:

    print "Buy " + ", ".join(itertools.chain.from_iterable(shopping_list)) + "."
    

    This gives us:

    import itertools
    
    options = [
        ("Minced Meat Soup", {'minced meat', 'potatoes', 'frozen vegetable'}),
        ("Sunday Soup", {'chicken with bones', 'noodles', 'soup vegetable'}),
        ("Gulas", {'pork meat', 'food cream', 'potatoes', 'onion', 'frozen peas'}),
    ]
    
    print "What would you like to cook on weekend?"
    print "Here are the options:"
    for option, (name, values) in enumerate(options, 1):
        print str(option)+". "+name
    
    choose = raw_input("> ")
    
    try:
        shopping_list = [options[int(choice.strip())-1][1] for choice in choose.split(",")]
        print "Buy " + ", ".join(itertools.chain.from_iterable(shopping_list)) + "."
    except (IndexError, ValueError):
        print "Hmmm. No such food on the list."
    

    Which produces something like:

    What would you like to cook on weekend?
    Here are the options:
    1. Minced Meat Soup
    2. Sunday Soup
    3. Gulas
    > 1, 2
    Buy potatoes, frozen vegetable, minced meat, chicken with bones, noodles, soup vegetable.
    

    This is short, easy to expand on, and functions well. There are some other issues you could deal with (how do you want to handle multiple of the same item? You might want to look into collections.Counter for that), but it’s the basic idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.