There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
def check_for_name(sentence, name):
for i in sentence.split():
if i.lower() == name.lower():
return True
for y in sentence.split():
if sentence.find(name) == -1:
return False
This was my solution. Not at all streamlined like the solution provided by CC.
I wrote a code that was not as streamlined as CC’s but resulted in expected returns. Yet, it didn’t seem to be read as correct by CC to unlock next task. Is there any way to see what is wrong with my code?
In the second example the grouping will result in a boolean.
if name in True:
That should tell us something. Notice in the first example each operand is a separate boolean. From left to right, the first one that evaluates to True will be the one that short-circuits the expression giving a True result.
Oh, but there is… They have a truth value known as, truthiness which opposite is falsyness.
Since we can assume that all the operands are non-empty strings, which are truthy. the evaluation will end on the first operand and yield True for the whole expression.
name in sentence_upper
|-------A boolean------|
sentence_upper
|truthy or falsy|
Short-circuiting
T or T or T or T
\
first True short-circuits
The latter three operands are not evaluated.
F and F and F and F
\
first False short-circuits
Understand that operands are expressions, not statements. All expressions boil down to a value. Even literals are technically expressions.
0
1
''
'a'
ans so on, though we seldom refer to them as expressions and usually call them values.
a + b
b - a
a < b
a == b
All the above yield a value, which is what the term, evaluate means.
a or b
a and b
Also expressions that yield a value upon evaluation. First the operand on the left is evaluated, and if truthy (for or) that will be the term yielded. Only if it is falsy is the second operand evaluated, if necessary, else the term yielded. When the first term of an AND expression if falsy, that becomes the yielded term. if truthy then the second term is the yield.
The above does not apply when written in if or while statements. The terms are not yielded, but a boolean result.