FAQ: Code Challenge: String Methods - Check Name

I see - fantastic!

I am screenshotting this adding to my notebook.

Thank you mtf!

1 Like
>>> 1 and 0
0
>>> 0 and 1     <= short-circuit
0
>>> 1 and 2
2
>>> 2 and 1
1
>>> 'red' or 'blue'     <= short-circuit
'red'
>>> 'red' and 'blue'
'blue'
>>> "" or 'blue'
'blue'
>>> "" and 'blue'     <= short-circuit
''
1 Like
def check_for_name(sentence, name):
  return(sentence.lower().find(name.lower())>=0)

Hi,

This was my solution, but it is not correct, it should return false for the 3rd print, mine returns true. Can somebody tell me why?

def check_for_name (sentence, name):

  words = sentence.split(" ")

  for word in words:

    if word.upper() or word.lower() or word.title() == name:

      return True 

  return False



print(check_for_name("My name is Jamie", "Jamie"))

# should print True

print(check_for_name("My name is jamie", "Jamie"))

# should print True

print(check_for_name("My name is Samantha", "Jamie"))

# should print False

this won’t work:

if word.upper() or word.lower() or word.title() == name:

word.upper() and word.lower() aren’t compared to anything. So the strings are simply evaluated as truthy (non-empty strings are seen as true)

so your condition will simply always give true.

What @stetim94 is saying is,

 'STRING' or 'string' or 'String'  =>  True

so unless name == True it will never give a True outcome.

It also will only ever be a comparison of a boolean to a string because the overall expression on the left will only yield a boolean.

D = name
A = word.upper()
B = word.lower()
C = word.title()

if A == D or B == D or C == D

Notice how the comparisons are written into each operand of the expression? That is the only way we can compare multiple values.

This is my solution but the logic of adding “False” seems to be not working,

def check_for_name(sentence, name):
  for i in sentence.lower().split():
    if i == name.lower():
      return True

it works for True statements and produces None for False statement.

def check_for_name(sentence, name):
  for i in sentence.lower().split():
    if i == name.lower():
      return True
    return False

The above code will somehow change all my results to False. Am I missing any logical interpretation here?

you nested return False within the loop. So now, only when the first word in the sentence equals name, we get true. Otherwise we get False.

return literally does what it says: returning/handing back data to the caller. Which signals that the function is done executing. Even if that means breaking the loop

so your loop currently only makes a single iteration, given a return keyword is reached within this iteration.

1 Like

I shift my return False indentation to left and it works. Is this the correct solution or I just happen to stumble into the correct answer. I added else clause to confirm my solution.

def check_for_name(sentence, name):
  for i in sentence.lower().split():
    if i == name.lower():
      return True
  else:
    return False

however, shouldn’t if-else clause be on the same indentation line?

Its very important to understand what implication changing the indention has on your code

then you would have the same problem as before, now you have a for/else (yes, that is also possible)

1 Like
def check_for_name(sentence, name):
  if (sentence.lower()).find((name.lower())) >= 0:
    return True
  return False 
1 Like

Please explain exactly how ‘return name.lover() in sentence.lower()’ works. If it tests for membership, does it test for all other ‘name’ variations returning ‘True’?

name may take any form when passed in to the function, as can sentence. Reducing both to lowercase before testing levels the playing field for simple comparison.

I considered whether or not if it was simply a matter of reasoning and deduction. Thank you for clarifying.

1 Like

why is my code returning True in the last check? Could somebody help me understand where did I get it wrong?

# Write your check_for_name function here:
def check_for_name(sentence, name):
  if name or name.upper() or name.lower() in sentence:
    return True
  elif name not in sentence:
    return False
# Uncomment these function calls to test your  function:
print(check_for_name("My name is Jamie", "Jamie"))
# should print True
print(check_for_name("My name is jamie", "Jamie"))
# should print True
print(check_for_name("My name is Samantha", "Jamie"))
# should print False

here:

name or name.upper()

no comparison is happening, so python will simply evaluate these values as truthy (or false)

So how do i correct it?

Consider,

    a = 'name'
    b = a.upper()
    c = a.lower()
    if a or b or c: # do something

Now since a is not an empty string, what will be the outcome?

True

It should be,

    if a in sentence or b in sentence or c in sentence:
        # do something

Hi guys, I don’t really understand why we have to include “.lower” and “.upper” with “sentence” like this:
if name.lower() in sentence.lower():
return True

Why can’t we just type it out without the .upper and .lower appendages like this?
if name.lower() in sentence:
return True

Appreciate the help

That reduces it to a level playing field, everything lower case. name and sentence are still in their original shape, but there is less to compare within the transformation.

>>> sentence = "Bill has a great big Newfoundlander named 'Tiny'."
>>> name = 'Bill'
>>> name.lower() in sentence.lower()
True
>>> name = 'BILL'
>>> name in sentence
False
>>> name.lower() in sentence
False
>>> name.lower() in sentence.lower()
True
>>>