#Python / Bob

10 messages · Page 1 of 1 (latest)

dusk flame
#
def response(hey_bob):
    #question
    if hey_bob.endswith("?") == 1 and hey_bob.isupper() == False:
        return 'Sure.'
    #yelling
    if hey_bob.isupper() == True and hey_bob.endswith("?") == 0:
        return "Whoa, chill out!"
    #yelling question
    if hey_bob.isupper() == True and hey_bob.endswith("?"):
        return "Calm down, I know what I'm doing!"
    #silence
    if hey_bob.isspace() or hey_bob == "":
        return "Fine. Be that way!"
    #anything else
    else:
        return "Whatever."

Hi! I'm really green when it comes to coding and I'm struggling to find a way to overcome this exercise. I can't deal with this one test:

** Test 23 Bob > ending with whitespace**
Code Run

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

Test Failure

AssertionError: 'Whatever.' != 'Sure.'

  • Whatever.
  • Sure.

Help highly appreciated! :D

winter igloo
#

Do you understand what this test is doing and what it expects?
Do you understand why that differs from what your code returns?

dusk flame
#

I think understand that I need to somehow capture the "? " part, and my best shot was to do

if hey_bob.isspace() or hey_bob == "" or "? " in hey_bob
  return "Fine. Be that way!"

however it only leads to more unsuccessful tasks in:

Code Run

self.assertEqual(response("Ending with ? means a question."), "Whatever.")

Test Failure

AssertionError: 'Fine. Be that way!' != 'Whatever.'
- Fine. Be that way!
+ Whatever.

and

Code Run

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

Test Failure

AssertionError: 'Fine. Be that way!' != 'Sure.'
- Fine. Be that way!
+ Sure.
#

i need to somehow capture the whitespace I think, but all of my attempts with .split() command failed :(

winter igloo
#

str.split() is a good instinct

#

But that splits the string into a bunch of words, splitting on whitespace

#

Do you need to split the line? Or is there something else you need here?

dusk flame
#

I can't quite tell

dusk flame
#

Oh, I've solved it! :D
I've decided to totally redo the code, starting by stating hey_bob = hey_bob.strip(). This way I, somehow, managed to deal with all silences and whitespaces. Everything else was just a matter of trials and errors but eventually i did it