Sean Sean - 5 months ago 14
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':
years=y_b-c_y
print('U r',abs(years),'jahre alt')
elif ans=='No' or 'no':
print("How old r u?")
else:
print('Sorry')

main(2012,2016)

Answer

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

>>> bool('yes')
True

You should instead test with:

if ans in ('Yes', 'yeah', 'yes'):
    # code
elif ans in ('No', 'Nah', 'no'):
    # code
else:
    # more code
Comments