[Bob exercise] IndexError: string index out of range

I’m having trouble figuring out the best approach for handling both an empty string as well as any of the added spaces or escape characters. It seems to be failing due to “IndexError: string index out of range”, as opposed to simply not recognizing my conditions.

My rationalize was to look for strings with two consecutive spaces " ", or any that included escape character from the original input. Even when testing the empty string, I get this error, but from what I’ve been learning, I don’t understand why this simple truthiness test fails:

hey_bob = ""
if not hey_bob:
        print( "Fine. Be that way!")

If hey_bob is “”, then should that be False, which means ‘not hey_bob’ should be True, therefore execute the print code?

Here is my working code:

"""Programming response from Bob"""

def response(hey_bob):
    """Function determinds Bob's answer depending on input"""
    original_input = repr(hey_bob)
    hey_bob = " ".join(hey_bob.split())
    white_space = ("  ", "\n", "\t", "\r")
    if hey_bob[-1] == '?' and not hey_bob.isupper():
        return "Sure."
    if hey_bob.isupper() and hey_bob[-1] != '?':
        return "Whoa, chill out!"
    if hey_bob.isupper() and hey_bob[-1] == '?':
        return "Calm down, I know what I'm doing!"
    if any(item in original_input for item in white_space) or not original_input:
        return "Fine. Be that way!"
    else:
        return "Whatever."
        

If the string is empty, this accessing a non existing element, which is an error. Consider replacing it with a built in string method.

Ah, that makes total sense. I wasn’t even thinking of that section since it passed the test on other sections. I will try to find another way to evaluate the last character of a string other than using negative indexing.

There are a bunch of really useful string methods, some of which overlap the functionality of using something[-1].

Binary and Sequence Types Summary

The Python track also has a couple of concept exercises that run through strings and string methods Little Sister’s Essay & Little Sister’s Vocab.