Reputation: 13
im trying to give different outputs depends on inputs, but i will get the same output for all inputs.
i tried to remove the "or" operator, and the code will work fine! but when using the "or" operator again, the code wont work the way it should
voroodi = input('Enter Something: ')
if voroodi == 'h' or 'H':
print('hello')
elif voroodi == 'g' or 'G':
print('Goodbye')
i expect to get output of "Hello" when i enter the "H" or "h" as input and "Goodbye" for "G" "g".
but im just getting hello for any input that i enter!
Upvotes: 0
Views: 1692
Reputation: 17892
You can use the operator in
:
voroodi = input('Enter Something: ')
if voroodi in 'hH':
print('hello')
elif voroodi in 'gG':
print('Goodbye')
Upvotes: 0
Reputation: 178021
or
doesn’t work that way. You must say;
if voroodi == 'h' or vortoodi == 'H':
’H’
all by itself is considered True
. Any non-empty string is considered true, so your original statement is if voroodi == 'h' or True:
which is always true.
Upvotes: 1
Reputation: 378
It should be
if voroodi == 'h' or voroodi == 'H':
print('hello')
elif voroodi == 'g' or voroodi == 'G':
print('Goodbye')
Rakesh solution will also worl\k
Upvotes: 0