0
list = [yes, no, seven]

print ("What do you want to pull from  the list?")
answer = input()

print (list[answer])

How do i do this kind situation? I know this example doesnt work, but how does one make it so that it does work?

Edit: I would rather it be a number I input, 0 for yes, 1 for no and 2 for seven if that is possible

  • possible duplicate of [How to delete an item in a list if it exists? (Python)](http://stackoverflow.com/questions/4915920/how-to-delete-an-item-in-a-list-if-it-exists-python) – Bhargav Rao Jun 11 '15 at 22:21

3 Answers3

2

You're looking for a dict, not a list.

my_dictionary = {'yes':1, 'no':4, 'seven':9}

answer = input("What do you want to pull from the dictionary? ")

print(my_dictionary[answer])
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0
list_ = [yes, no, seven]

print ("What do you want to pull from  the list?")
answer = input()

print (list_[list_.index(answer)])
farhawa
  • 10,120
  • 16
  • 49
  • 91
0

First, define the list of items properly:

>>> items = ['yes', 'no', 'seven']

Note that I call it items, because list is a built-in function.

Now get the input:

>>> answer = input("What do you want to pull from  the list? ")

At this point, answer is a string. But a number is needed for an index in the list of items, so it must be converted using the built-in int function:

>>> index = int(answer)

Now you can print the item at the given index:

>>> print(items[index])
ekhumoro
  • 115,249
  • 20
  • 229
  • 336