Comparing Strings

Thank you, I will keep that in mind!

Look again at the documentation for all(), because it also shows the equivalent code without using all():

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

You can see it only uses for, if, and return. See if that helps you figure out how to write your own function.

Wow, that is extremely helpful. Would that be able to be used to compare each character in a string to another string?

Yes, this is a very common pattern and can be adapted to the function is_word_guessed you’ve asked about.

Others have already shared the “short and sweet” ways Python can solve this, like @kknechtel:

Instead, I am going to write a version that is long and verbose on purpose, because Python’s list comprehensions and helpful operators like in can hide a bit of what is happening “under the hood”, and it’s nice to know that too.

You can break this apart by seeing that there is something that needs to be repeated:

“all the letters of secret_word” :arrow_forward: we’re going to check something for each letter in secret_word, one at a time

and what the thing is that we’re going to repeat:

“…are in letters_guessed” :arrow_forward: we need to be able to check if one letter is in the list “letters_guessed”.

So first, how do we check if something is in a list?

  • Look at every item in the list
  • If the item matches the thing we’re looking for, then it was in the list. We don’t need to look at the rest of the list
  • If we look at every item and never see a match, then it wasn’t in the list
def search_in_list(search_for_this, list_of_things):
    # look at each item in the list
    for one_thing in list_of_things:
        # check if it matches what we're looking for
        if search_for_this == one_thing:
            # if it does, we don't need to keep looking
            return True
    # we went through the whole list, but nothing matched
    return False

So now we should be able to do this:

letters_guessed = ['a', 'b', 'c']

search_in_list('a', letters_guessed)
# => True

search_in_list('d', letters_guessed)
# => False
More about searching in lists

You never actually have to write search_in_list yourself (after writing it once to see how it works). Looking for something in a list is so common that every programming language I’ve ever used has had a function already built in that does this. They might each call it something different (find, contains, index_of, etc.) but it would be weird for it not to be there at all.

In fact it’s so common, that Python built it into the language. If you use the keyword in like this:

'a' in letters_guessed

It does exactly what search_in_list does. That expression will return True if 'a' appears in the list letters_guessed, so you can use it as a condition:

if something in some_list:
    # it was in the list!

And if you use not in, it will do the opposite, and return True only if the item isn’t in the list.


Now we need to do this for every letter in secret_word. A very nice thing about Python is that it will let you use a string just like a list of characters without any extra work at all, so you can do something like this:

for letter in "abcdefgh":
    print(letter)

and it will print each letter on its own line. This is nice for us because we don’t need to do anything special to look at all the letters in secret_word one at a time - we can just loop over them:

for letter in secret_word:
    if search_in_list(letter, letters_guessed):
        # ???

The last thing to do is get our logic straight. We need something to be true for all the letters. If even one of them is not in the list, we want to return False.

def is_word_guessed(secret_word, letters_guessed)
    ok = True
    for letter in secret_word:
        if search_in_list(letter, letters_guessed):
            # the letter is in the list, so far so good
        else:
            # this letter wasn't in the list!
            ok = False
    return ok

Since we don’t need to do anything when our search is successful, only when it fails, we can make this a lot simpler:

def is_word_guessed(secret_word, letters_guessed)
    for letter in secret_word:
        if not search_in_list(letter, letters_guessed):
            # this letter wasn't in the list!
            return False
    return True
More about checking conditions on list items

Like in, this is also so common that most languages have a function for it. It is very common to need code like this:

for item in items:
    if not check_something(item):
        return False
# all the items passed the check
return True

or like this:

for item in items:
    if check_something(item):
        # at least one item passed the check
        return True
return False

Python calls its helpers for this all (every test must pass) and any (at least one test must pass), but you might also see functions like some or every.


And all of that is why its pretty cool that in Python you can write:

all(c in letters_guessed for c in secret_word)
2 Likes

Sorry for the late response, been busy at work and this is a side project while I have a light load, but thank you very much for the in-depth response and explanation. I haven’t had time to read it through properly, but I will!