Bob Exercise - 1 test failed

Hello
I am working on the Bob exercise. This is my code

def response(hey_bob= None):
    if hey_bob == None or not hey_bob.strip():
        return "Fine. Be that way!"
    elif hey_bob.isupper() and hey_bob[-1] == "?":
        return "Calm down, I know what I'm doing!"
    elif hey_bob[-1] == "?":
        return "Sure."
    elif hey_bob.isupper():
        return "Whoa, chill out!"
    else:
        return "Whatever."

It seems to work… almost. 24 tests passed, but one failed:
Test 23 - Bob > ending with whitespace

response("Okay if like my  spacebar  quite a bit?   "), "Sure."
AssertionError: 'Whatever.' != 'Sure.'

This is strange behaviour. Why does it return “sure”? hey_bob[-1] should return a whitespace in this case so that the condition of “sure” is not fulfilled.

I have copy & pasted my code in an online-compiler (programiz.com) and when I test it with:

print(response("Okay if like my  spacebar  quite a bit?   "))

It returns correctly “Whatever”.

The problem specs specify:

    {
      "uuid": "05b304d6-f83b-46e7-81e0-4cd3ca647900",
      "description": "ending with whitespace",
      "property": "response",
      "input": {
        "heyBob": "Okay if like my  spacebar  quite a bit?   "
      },
      "expected": "Sure."
    },

The Python tests align with those specs:

    def test_ending_with_whitespace(self):
        self.assertEqual(
            response("Okay if like my  spacebar  quite a bit?   "), "Sure."
        )

The sentence should be considered a question when they end with a ?. Trailing whitespace should not count and ending with "? " should be considered ending with a ?.

Oh, I see, in other words I misunderstood the error message a bit. My program did not return “Sure” but “Whatever” instead, but “Sure” is what is actually expected. In this case I have to modify my code accordingly.
Thx

Wow, now after I completed this exercise, I see that there is even a special badge for completing this exercise. Only 7.4% of all Exercism users have earned this badge. Wow, this makes me feel good and gives me some motivation. Not bad for a complete beginner like me. :innocent:

1 Like

It’s a famous Exercism exercise :slight_smile: