0

I am new to programming and am trying to update a pre-established list based upon user input in Python 2.5.

Here is exactly what I am trying to do:

I have various items that a user must choose from...let's use the following as an example:

item1 = [1,2,3,4]
item2 = [2,3,4,5]

I have the user choosing which item by using raw_input:

item_query = raw_input("Which item do you want?: ")

Once they have chosen their appropriate item, I want to place the appropriate item (along with the items contained in that respective list) into a blank list that will maintain that user's inventory:

user_inventory = []

Can anyone show me how to do this?

1
  • 1
    What would the user enter if he wants item1? 1? item1? Commented May 29, 2014 at 16:22

1 Answer 1

1

you should use a dictionary where the keys are the input you expect the user to enter

items = {"1":[1,2,3,4],"2":[2,3,4,5]}
user_inventory = []
while True:
   item = raw_input("Which Item would you like?")
   if item == "" or item.lower() == "q" or item.lower() == "quit":
       #user is done entering items
       break
   if item in items:
       #this is a little dicey since it is actually the same list, not a copy of the list
       user_inventory.append(items[item]) #you may want to insert a copy of the list instead
   else:
       print "Unknown Item:%s"%item
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best aproach, and you could add an else statement to tell that the item is not in your dict

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.