Printing out Python list

Question:

drinks = ['cola', 'water', 'beer', 'wine']

drinks = raw_input ("What did you drink? " + print drinks)

So, basically I want the program to show user the question and after that print out the possible answers. The user should type one of them back into the program. I tried the code like above but it doesn’t work. Printing drinks before raw_input kinda doesn’t make sense. I guess you could also go with it like that:

drinks = ['cola', 'water', 'beer', 'wine']

drinks1 = raw_input("Hai, wanna have a look at the most popular drinks here?")
if drinks1 == "yes":
print drinks

drinks2 = raw_input("So, what did you drink?")

…but is there any simpler to do lists like that in Python, that user would choose from?

Thanks in advance for answer.

Asked By: Karolina

||

Answers:

A very simple solution could be

drinks = ['cola', 'water', 'beer', 'wine']

drink = raw_input ("What did you drink? %s" % ', '.join(drinks))

if drink in drinks:
    print('you chose ' + drink)
else:
    print('invalid choice')

Use join to make a string out of the list.

Answered By: Paul Rooney

You can write a generic program by using strip and lower functions. Everything else looks okay.

drinks = ['cola', 'water', 'beer', 'wine']

user_input = raw_input("Hai, wanna have a look at the most popular drinks here?")

if user_input.strip().lower() == "yes":
   print drinks

drinks2 = raw_input("So, what did you drink?")
Answered By: python
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.