Pol Hallen Pol Hallen - 4 years ago 118
Python Question

how to simplify if statement yes

What's the best way to simplify this if?

#!/usr/bin/python

ans=input("choose yes or no: ")

if ans == "yes" or ans == "YES" or ans == "y" or ans == "Y":
print("ok")
else:
print("no")

Answer Source

You could check it with list:

if ans in ['yes', 'YES', 'y', 'Y']:
    print('ok')
else:
    print('no')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download