-2

So my teacher asked me to find whats wrong with this program, but i cant fix it. The problem in the program is that it always prints the yes output, no matter what the input is. So pls help. And please be clear.

Here's the source code:

a = raw_input('>')

if 'y' or 'Y' in a: print 'you entered yes'

elif 'n' or 'N' in a: print 'you entered No'

else: print 'Try again !'
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Arvind M.
  • 67
  • 1
  • 6

1 Answers1

2

I think you meant (easier to check if in a sequence of valid values):

if a in ('y', 'Y'): 
    print 'you entered yes'
elif a in ('n', 'N'): 
    print 'you entered No'

At the moment you are misunderstanding boolean operations. At the moment it is evaluated as for example:

if ('y') or ('y' in a):

which is clearly not what you want. It will be always come out because as long as a string is not empty it is True, so that is what is returned due to short-circuit evaluation.

anon582847382
  • 19,907
  • 5
  • 54
  • 57