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

The output in the console is what we would get if printing a function reference…

print (letter_check)

We cannot see if that is what is in the editor. On line 7, add a print() so your return value echoes to the console.

print (letter_check("strawberry", "a"))

The bigger question here is, where does character come from?

1 Like

It doesn’t specify to how you should test in the Instructions.
Only when you run it then it informs that there should be specific test which you’ll find in hidden in hint.
This should be fixed.

2 Likes

I was literally thinking the exact same thing. Like who wrote this? It doesn’t follow the same logical learning pattern as earlier in the path.

2 Likes

I did it like this guys

def letter_check(word, letter): count = 0 for a in word: if a == letter: count = count + 1 if count > 0: return True else: return False print(letter_check("strawberry", "a")) print(letter_check("strawberry", "o"))
1 Like

Hello everyone,
I was trying to complete this exercise and although I am getting the correct output when printing letter_check, the code is not getting accepted and I was wondering why?

``` def letter_check(word, letter): if letter in word == word: return 'True' else: return 'False' print(letter_check("strawberry", "y")) ```

thank you in advance to who will reply!

You are returning strings 'True' and 'False'.

Instead, booleans should be returned. Booleans True and False don’t have quotes.

Your solution isn’t being accepted for the above reason, but as an aside, the condition

if letter in word == word:

is not necessary. It is being evaluated as:

if (letter in word) and (word == word):

The second operand (word == word) will always be True, so effectively the condition simplifies to:

if letter in word:
1 Like

thank you so much for the clarification, it makes so much sense now :slight_smile:

That was a clever way of doing it!

I was commiting the same mistake. In the code you provided the function will return True or False depending on the first letter of the word.

As the word is “strawberry” it will return False.

As it is, the code iterates only with the first letter and returning the result.

So to fix it the program should run through the whole word. For that you should place the return clause outside of the loop:

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

1 Like

@jacielbarros well said and a common mistake for sure. The key thing people need to remember is that return statement ends the function. So with the else you are guarantee to end the function after the first iteration.

I ended up using the given solution, but there’s something I don’t understand about how it works. From what I understand, python code from the top down. So with the given layout of the code, if the desired letter is found within the given word, how is the True boolean not immediately overwritten with the False boolean once the loop completes? From what I know, the return statement doesn’t automatically break the loop, and even if it did, the “return False” line is outside of the loop. So shouldn’t the function return False every time with this layout?

[/codebyte]
def letter_check(word, letter):
for char in word:
if char == letter:
return True
return False

How couldn’t it? It exits the function, immediately, with the value True in tow.

In the above example, return False would be the default, assuming the loop has fully executed without interruption.

1 Like

I guess that’s what I meant. I didn’t know that it exits the function. But that answers my question then. Thank you.

1 Like

We have one simple rule as pertains to, return:

Anything after return is unreachable.

That would include the loop that contains it. The focus is on the payload the caller will see.

Suggest use the </> tool to post code samples so we can see the indentation used in Python code. It is meaningful and important.

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

When you post code that looks like this, we can make something of it. The stuff in your earlier post is pure gibberish, in Python terms.

2 Likes

Thanks for the tip. I thought when I used the Codebyte tool it would include indentations, sorry.

1 Like