Learn Python3 - Coding Challenges II: Count unique letters

Hello, I’ve just started the coding challenges II on the Learn Python3 beginner course. I was just wondering if anyone can help me understand the way Codecademy have answered it… They use the following to count unique letters in any given word


def unique_english_letters(word):
  uniques = 0
  for letter in letters:
    if letter in word:
      uniques += 1
  return uniques

How does this keep track of letters that have already been counted exactly? It works, I just don’t understand how it’s working. Should the counter not raise at all instances of each letter? To perhaps make it easier to visualise how I’m looking at it, this is what I done.

def unique_english_letters(word):
  count = 0
  counted_letters = []
  for letter in word:
    if letter in letters:
      if letter not in counted_letters:
        count += 1
        counted_letters.append(letter)
  return count

Thanks for taking the time to help me out with this, just want to understand what I’m missing! :slight_smile:

Hi,
I assume letters is a string or list of all the letters of the alphabet (e.g “abcdefgh…”)
If it is, then what it’s doing is checking if each letter from that string is in word one by one. If it is, then no matter how many times it’s there it’ll just add one.

So, for example
word = “banana”
the loop would go;
is there an ‘a’ in there ? yes, add 1 to uniques
is there a ‘b’ in there ? yes, add 1 to uniques
is there a ‘c’ in there ? no
etc

hope that helps

Thank you, apologies for the late reply and mark. Away for the Easter weekend :slight_smile:
This makes sense, I just assumed that it would add multiple to the unique count for each occurrence of it. Handy to know.