FAQ: Introduction to Strings - Strings and Conditionals (Part One)


#1

This community-built FAQ covers the “Strings and Conditionals (Part One)” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Strings and Conditionals (Part One)

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Why don’t we use - else: - here?

def letter_check(word, letter):
for character in word:
if character == letter:
return True
else:
return False


#3

Because when you do character == letter, it will only check the first character. It has to do with understanding return statements. When you have a return statement the loop will stop. For example:
if you are checking if the letter “m” is in ice-cream, your code will do this:

Loop through first character. The character does not equal to letter(“m”). Now it it will move to the else statement which is False. Now it will return False and end the loop.

Say you now want to search for “i” in ice-cream, you would start with the first character and check if it is “i.” If your code runs, it will see that the first character is I and return True. Now your answer is True which is technically correct and your entire loop would stop because of return statement.

This is why it would be a good idea to use continue which is what I did. I will let you try to play with that. I am not sure how codecademy solved that question though


#4

Thank you for answering.
Codeacademy provides with a solution like this:

def letter_check(word, letter):
for character in word:
if character == letter:
return True
return False

My question would be , wouldn’t that return True (when requirement character==letter is met) AND False always? Because False is outside the loop. So the code always sees it and executes it. Right?


#5

Ok so here is how the codecademy function works:

This is how I can help you visualize it:

def letter_check2(word, letter):
for character in word:
print(character)
if character == letter:
return True
return False

Notice I put the print statement right after the for loop. This is just to visualize what happens:

Say we want to find “l” in “hello”

If we run print(letter_check2(‘hello’,‘l’)), we get
h
e
l
True

so the print prints out h then e then l put notice it does not go to the second “l.” It stops at the first “l” and returns True as the value. It loops through every character until it finds True.

Now as for the False statement. The False only runs if True does not return from the for loop. This means that the function will have to do all of its necessary loops before it Returns False. The False value is not outside of the Function, it is outside of the for loop. This means that it will not be able to return True if it does not find the character you are looking for.

Remember what I said before; a return value ends a function. So if True is not returned from the for loop, the function will inevitable run False. However, if it returns True the function ends, and the for loop stops looking for any other character.

Hope that helps.
Sorry I wrote a lot haha


#6

Thank you so much ,
it all makes sense now.
This is what I understood: if the code returns True, it doesn’t actually care about the “return False” written after that. This is because a return value ends the function, before it sees the return False which is written afterwards.


#7

Yep you got it. Glad it helped :slight_smile:


#8

Hello, this exercise has highlighted something that I haven’t exactly understood about Python programming, but up until this point I couldn’t put my finger on it… I’m having trouble grasping how python interprets indentation levels. In the following example:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
_return False

The code returns True if the letter is found in the word. All good so far.

If I put a single space in front of ‘return False’, the program throws an indentation error. Not sure why it has to see everything spaced twice, but not my issue here. If another space is added, yielding:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
__return False

the program now returns ‘False’ with the same input. With another two spaces before ‘return False’, i.e.:

def letter_check(word, letter):
_for character in word:
__if character == letter:
___return True
___return False

The program now returns ‘True’ again with the same input. I guess what I’m asking is how should my brain be interpreting the indentation levels here? I should probably straighten this out before things get much more complex…

Thanks in advance,
Joe

edit: replaced spaces with underscores to highlight issue


#9

Hi,

First, if you </> for Preformatted text it makes it easier to read:

def letter_check(word, letter):
  for character in word:
    if character == letter:
      return True
  return False

In your second code it will only return True if the first letter of the word is the letter. Else it will continue the loop. If character == letter is not True so it continues to your next line, return False.

Your last code is only working correctly if the letter is in the word. You will never get False because at the moment the if statement is True it will go to Return True. If the statement is false it will go back to the for loop. So there is no Return statement if the letter is not in the word and it will print out “None”.

I hope this helped!

Edit: If you put in the code in here you can visulize it and you see the difference in the different Return False placement.