Sean Sean - 1 year ago 88
Python Question

The primitive codes of python

How I can overcome an issue with conditionals in python? The issue is that it should show certain text according to certain conditional, but if the input was No, it anyway indicates the data of Yes conditional.

def main(y_b,c_y):
ans=input('R u Phil?')
if ans=='Yes' or 'yes':
print('U r',abs(years),'jahre alt')
elif ans=='No' or 'no':
print("How old r u?")


Answer Source

or is inclusive. So the yes test will always pass because when ans != 'Yes' the other condition yes has a truthy value.

>>> bool('yes')

You should instead test with:

if ans in ('Yes', 'yeah', 'yes'):
    # code
elif ans in ('No', 'Nah', 'no'):
    # code
    # more code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download