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

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!

9 posts were merged into an existing topic: Why doesn’t else: return False work?

2 posts were split to a new topic: How does the indentation level of return False change the code?

2 posts were split to a new topic: How does the variable in a for loop get defined?

why does the hint say “Try running py letter_check(“strawberry”, “a”) and py letter_check(“strawberry”, “o”)”?

What does run py mean?

def letter_check(word, letter):

for character in word:

if character == letter:

  return True

else:

  return False

print(letter_check(“strawberry”, “a”))

Hello, can someone please explain why there is an error if I use ‘else’? Thank you

The test are incorrect here

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

print(letter_check("test", "testing"))
print(letter_check("test", "nothing"))

Return True on first print, False on second print. Instead I get…

Expected the test letter_check("strawberry", "a") to return True .


Again, this is a membership test.

return letter in word

In this and your other post the arguments are switched around. Would a word be in a letter?

How is this incorrect yet the simple test I posted reveal that it is indeed the correct output? Reason I did it offline that way is to show that it is the test that is failing on a correct output.

How does that make any sense?

return letter in word

The worst thing a learner can subject themselves to is correction bias. It clouds our thinking.

Same question here. If I try to do this, I get Syntax:Error: invalid syntax in the far right window.

Hi there,
I know that correct code is:

def letter_check(word, letter):

for i in word:

if i == letter:

  return True

return False

But why below version always return False ?

def letter_check(word, letter):

for i in word:

if i == letter:

  return True

  break

else:

  return False
1 Like

Be smart. Use less code
return (letter in word)

1 Like

This because you below code is iterating on word for each letter and in doing so it first takes the first first letter of word and matching it (because of if statement) with letter argument passed to function if it matches then it is going to read the code block inside if statement and since it find to return True so it return True and then breaking the loop. On contrary, if in the very first iteration, the first letter of word don’t matches with letter argument then it directly jumps to else statement and it return False. You need to thing the execution steps of your code then you’ll easily understand. Hope it helps.

1 Like

I have a similar question. I wrote the following:

def letter_check(word, letter):
  if letter in word:
    return 'True'
  else: return 'False'

print(letter_check(‘strawberry’, ‘p’)) <–gives False
print(letter_check(‘strawberry’, ‘a’)) <-- gives True

The CA system didn’t like it, and I saw their answer had a lot more steps:

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

Since my code worked, is there a reason why we need to include the “for” line in here? Are there some cases I’m not thinking of where my code wouldn’t work?

One could suggest the author is still giving the learner practice with loops , conditionals and return. The first example should not be returning string objects. Boolean primitives are not strings, as we see in the second example.

Truth is we don’t need the loop, nor the conditional, nor the literals, just a boolean expression…

 return letter in word
1 Like

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

test = letter_check(“boar”, “b”)

me hopes everyone is having a fantastic day!

cheers!

2 Likes

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

print(letter_check(“bedroom”, “m”))

I included print(character) to see why I was getting a False result. The loops keeps stopping at “a”. Can I get an explanation why?

To me, my code should do the same thing as the code solution. The only thing different is that I include else? Right?

All my indentions are right too, not sure how to format in indentions. I guess, how do i do that too?

Remember that once a return statement is executed, the function is immediately exited. This means that any other code in the function will not be run once a return statement occurs. Can you see the problem in your code? Hint: Your loop currently only goes through 1 iteration no matter what the result of the if statement is.

1 Like

What am I supposed to do differently?