0
Beginner, Trouble with OR operators
Code: print('How old are you?') age=int(input()) if age<21: print('You\'re underaged? get out of my bar!') if age>=21: print('Alright! ' + 'What would you like to drink?') drink=input() if drink =='beer': print('*pours beer*') print('There you are!') elif drink =='whiskey'or 'vodka' or 'alcohol' or 'rum': print('*pours shot of '+ drink + '*') print('There you are!') else: print('We don\'t serve that here') Issue: My problem is that you can input anything as a drink and it will print "print('*pours shot of '+ drink + '*')". so you can input 'cheese' and my virtual bartender will pour you a shot of cheese. Can someone explain why this is not working?
3 odpowiedzi
+ 7
I think the problem is in the
elif drink == 'whiskey' or 'vodka' or 'alcohol' or 'rum'
change it to
elif drink == 'whiskey' or drink == 'vodka' or drink == 'alcohol' or drink == 'rum':
you can also do
elif drink in ['whiskey', 'vodka', 'alcohol', 'rum']:
print('*pours shot of' + drink + '*')
And try again... If that doesn't fix it then I will try something else
+ 3
Your elif should be like this
elif drink == "whiskey" or drink == "vodka" or....
+ 3
@Utkarsh Sharma. That worked!
That makes sense, this was the first time I used OR operators.
I'm trying to update this little exercise code as I learn the new things to make it more concise, so I'll probably change it to the second thing you suggested once I get there.