2

I had this question on a recent test:

There are two wolves, a and b, and the parameters a_howl and b_howl indicate if each is howling. We are in trouble if they are both howling or if neither of them is howling. Return True if we are in trouble.

wolf_trouble(True, True) → True

wolf_trouble(False, False) → True

wolf_trouble(True, False) → False

My code was as below, and prior to submitting I tested that it worked on all three conditions.

def wolf_trouble(a_howl, b_howl):
    if a_howl == True & b_howl == True:
        return True
    elif a_howl == False & b_howl == False:
        return True
    else:
        return False

There was an additional test condition that wasn't mentioned however, and due to this, I only got partial credit. :

wolf_trouble(False, True) → False

wolf_trouble(False, True) returns True when I run my code, and I'm trying to understand why. Since I have set all conditions that aren't (True, True) or (False, False) to return False, why am I seeing this result?

Apart from hard coding every possible permutation, what steps can I take so that my code does take care of these conditions?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
user2762934
  • 2,332
  • 10
  • 33
  • 39

1 Answers1

4

& is the bitwise and operator. Instead you should have used and, which is the logical and operator. Note, BTW you could have simplified this function considerably by simply checking if a_howl and b_howl are equal:

def wolf_trouble(a_howl, b_howl):
    return a_howl == b_howl
Mureinik
  • 297,002
  • 52
  • 306
  • 350