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."