简体   繁体   中英

how to give different outputs depends on input in python?

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!

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.

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

You can use the operator in :

voroodi = input('Enter Something: ')
if voroodi in 'hH':
    print('hello')
elif voroodi in 'gG':
    print('Goodbye')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM