Reputation: 71
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.
Upvotes: 2
Views: 85
Reputation: 21619
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.
Upvotes: 1
Reputation: 4521
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?")
Upvotes: 0