0

I'm having a strange issue taking in user input, appending a list with the correct information, and checking the list for the correct input.

For some reason, only entering 'pally' calls end_instance.enter() (without 'polly').

def enter(self):
    print """Please enter your two passwords to proceed. Check the other doors for answers. You have five guesses."""

    green_count = 0

    green_ans = []

    while green_count < 5:

        green_guess = (raw_input('> '))

        if 'polly' or 'pally' in green_guess:
            green_ans.append(green_guess)
            green_count += 1

            if 'polly' and 'pally' in green_ans:
                print "cool"
                end_instance.enter()
        else:
            print "try again"
            green_count += 1
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
BarCode11
  • 1
  • 1
  • 2
    `if 'polly' or 'pally' in green_guess:` should be `if 'polly' in green_guess or 'pally' in green_guess:` Similarly for the `and` check – karthikr Nov 10 '15 at 01:18

2 Answers2

1

'polly' and 'pally' are both non-empty strings, and thus equate to True in boolean contexts in Python. Using or and and computes the boolean expression with short-circuit evaluation:

>>> 'polly' or 'pally'
'polly'   # first True value; second not checked
>>> 'polly' and 'pally'
'pally'   # last True value; both checked

So, the line:

if 'polly' and 'pally' in green_ans:

is equivalent to saying:

if 'pally' in green_ans:

Thus, only 'pally' works.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
0

'polly' will always be true.

Therefore, change your code to this:

def enter(self):
    print """Please enter your two passwords to proceed. Check the other doors for answers. You have five guesses."""

    green_count = 0

    green_ans = []

    while green_count  '))

        if 'polly' in green_guess or 'pally' in green_guess:
            green_ans.append(green_guess)
            green_count += 1

            if 'polly' in green_guess and 'pally' in green_guess:
                print "cool"
                end_instance.enter()
        else:
            print "try again"
            green_count += 1
Zizouz212
  • 4,908
  • 5
  • 42
  • 66