Introduction
Bob is a lackadaisical teenager.
He likes to think that he’s very cool.
And he definitely doesn’t get excited about things.
That wouldn’t be cool.
When people talk to him, his responses are pretty limited.
Instructions
Instructions
Your task is to determine what Bob will reply to someone when they say something to him or ask him a question.
Bob only ever answers one of five things:
- “Sure.”
This is his response if you ask him a question, such as “How are you?”
The convention used for questions is that it ends with a question mark.
- “Whoa, chill out!”
This is his answer if you YELL AT HIM.
The convention used for yelling is ALL CAPITAL LETTERS.
- “Calm down, I know what I’m doing!”
This is what he says if you yell a question at him.
- “Fine. Be that way!”
This is how he responds to silence.
The convention used for silence is nothing, or various combinations of whitespace characters.
- “Whatever.”
This is what he answers to anything else.
Dig Deeper
If statements
if statements
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if is_shout:
return 'Whoa, chill out!'
if is_question:
return 'Sure.'
return 'Whatever.'
In this approach you have a series of if statements using the calculated variables to evaluate the conditions.
As soon as a True condition is found, the correct response is returned.
Note that there are no `elif` or `else` statements.
If an `if` statement can return, then an `elif` or `else` is not needed.
Execution will either return or will continue to the next statement anyway.
The rstrip method is applied to the input to eliminate any whitespace at the end of the input.
If the input has no characters left, it uses the falsiness of an empty string with the not operator to return the response for saying nothing.
Since it doesn’t matter if there is leading whitespace, the rstrip function is used instead of strip.
The isupper method is used to test that there is at least one cased character and that all cased characters are uppercase.
A cased character is one which differs between lowercase and uppercase.
For example, `?` and `3` are not cased characters, as they do not change between lowercase and uppercase.
`a` and `z` are cased characters, since their lowercase form changes to `A` and ` Z` when uppercase.
The endswith method is used to determine if the input ends with a question mark.
The logical AND operator (and) is used to test a shout and a question together.
if statements nested
if statements nested
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
return 'Whoa, chill out!'
if is_question:
return 'Sure.'
return 'Whatever.'
In this approach you have a series of if statements using the calculated variables to evaluate the conditions, some of which are nested.
As soon as a True condition is found, the correct response is returned.
Note that there are no `elif` or `else` statements.
If an `if` statement can return, then an `elif` or `else` is not needed.
Execution will either return or will continue to the next statement anyway.
The rstrip method is applied to the input to eliminate any whitespace at the end of the input.
If the input has no characters left, it uses the falsiness of an empty string with the not operator to return the response for saying nothing.
Since it doesn’t matter if there is leading whitespace, the rstrip function is used instead of strip.
The isupper method is used to test that there is at least one cased character and that all cased characters are uppercase.
A cased character is one which differs between lowercase and uppercase.
For example, `?` and `3` are not cased characters, as they do not change between lowercase and uppercase.
`a` and `z` are cased characters, since their lowercase form changes to `A` and ` Z` when uppercase.
The endswith method is used to determine if the input ends with a question mark.
Instead of testing a shout and a question on the same line, this approach first tests if the input is a shout.
If it is a shout, then the nested if/else statement returns if it is a shouted question or just a shout.
If it is not a shout, then the flow of execution skips down to the non-nested test for if the input is a question.
Answer list
Answer list
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
"Calm down, I know what I'm doing!"]
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = 2 if hey_bob.isupper() else 0
is_question = 1 if hey_bob.endswith('?') else 0
return ANSWERS[is_shout + is_question]
In this approach you define a list that contains Bob’s answers, and each condition is given a score.
The correct answer is selected from the list by using the score as the list index.
Python doesn’t enforce having real constant values,
but the ANSWERS list is defined with all uppercase letters, which is the naming convention for a Python constant.
It indicates that the value is not intended to be changed.
`ANSWERS` could prevent item reassignment by being defined as a [tuple](https://realpython.com/python-lists-tuples/#python-tuples) instead of a list.
The items in a tuple cannot be changed, and the performance between a tuple and a list here is equivalent.
The entire `ANSWERS` tuple could still be reassigned to another tuple,
so uppercase letters would still be used to indicate that the `ANSWERS` tuple should not be changed.
The rstrip method is applied to the input to eliminate any whitespace at the end of the input.
If the input has no characters left, it uses the falsiness of an empty string with the not operator to return the response for saying nothing.
Since it doesn’t matter if there is leading whitespace, the rstrip function is used instead of strip.
A ternary operator is used for determining the score for a shout and a question.
The isupper method is used to test that there is at least one cased character and that all cased characters are uppercase.
A cased character is one which differs between lowercase and uppercase.
For example, `?` and `3` are not cased characters, as they do not change between lowercase and uppercase.
`a` and `z` are cased characters, since their lowercase form changes to `A` and ` Z` when uppercase.
If isupper is True, then is_shout is given the value of 2; otherwise, it is given the value of 0.
The endswith method is used to determine if the input ends with a question mark.
If the test for endswith('?') is True, then is_question is given the value of 1; otherwise it is given the value of 0.
The response is selected from the list by the index like so
| is_shout | is_question | Index | Answer |
|---|
false | false | 0 + 0 = 0 | "Whatever." |
false | true | 0 + 1 = 1 | "Sure." |
true | false | 2 + 0 = 2 | "Whoa, chill out!" |
true | true | 2 + 1 = 3 | "Calm down, I know what I'm doing!" |
Source: Exercism python/bob