Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Functions

Monkey trouble

#
# monkey_trouble.py
#

# This exercise is from CodingBat by Nick Parlante
# https://codingbat.com/prob/p181646

# We have two monkeys, a and b, and the parameters aSmile and
# bSmile indicate if each is smiling. We are in trouble if they are
# both smiling or if neither of them is smiling. Return true if we
# are in trouble.

# monkey_trouble() is the function you want to test.

def monkey_trouble(aSmile, bSmile):
    return aSmile == bSmile

# test_monkey_trouble() is the unit test function, with arguments
# for given input and the expected output.

def test_monkey_trouble(aSmile, bSmile, expected):
    result = monkey_trouble(aSmile, bSmile)
    
    print(f"aSmile: {aSmile}, bSmile: {bSmile}, "
          f"expected: {expected}, result: {result}") 

    if result == expected:
        print("PASSED")
    else:
        print("FAILED")


# We run several tests, varying the input parameters and checking
# that we get the output we expect from monkey_trouble().

test_monkey_trouble(True, True, True)
test_monkey_trouble(False, False, True)
test_monkey_trouble(True, False, False)

# Named arguments can help the readability of your code.

test_monkey_trouble(aSmile=False, bSmile=True, expected=False) 

Output:

aSmile: True, bSmile: True, expected: True, result: True
PASSED
aSmile: False, bSmile: False, expected: True, result: True
PASSED
aSmile: True, bSmile: False, expected: False, result: False
PASSED
aSmile: False, bSmile: True, expected: False, result: False
PASSED

Next: