FAQ: Code Challenge: String Methods - Count Letters

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

This doesn’t work, though the only difference with the solution is that “unique” becomes “uniques”.
What is the problem if the code stays coherent ? Is there something special to the word “unique”, or is it only some do-as-you’re-told thing ?
It’s not the first time that I have this kind of problem, it ends in a big loss of time and I still don’t understand the difference in the end.

Could someone enlighten me on that please ?

[Edit: It seems that the indentation of 'return" is wrong.]

Hello fellow coder!
I just completed the Count Letters challenge and was wondering what other ways people solved this question.

Here is my code:

letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”

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

If you have another way of doing this or any suggestions to improve the efficiency of my code, please post a reply.

Thank you and have fun coding,
Gabriel

One way to improve our code is to look for the shortest path. Do we iterate a 52 character string, or the word, itself. Usually the word will be shorter and take less iterations.

for letter in word:
    if letter in letters:

I was wondering if there was a way to do that, but wouldn’t the method you suggested repeated letters twice? In the problem we are trying to determine the number of unique characters, but for a word like “apple” wouldn’t it count “p” twice, this not returning the number of unique characters, but rather the number of total characters?

I only showed you the first two lines, there is still checking to be done. You may wish to create a string from the found letters with which to compare and only extend the string if the character is not present. Whether you use count or len() is up to you but we need the checklist (the string).

>>> def unique_english_letters(word):
    uniques = ""
    for letter in word:
        if letter in letters and letter not in uniques:
            uniques += letter
    return len(uniques)

>>> unique_english_letters('mississippi')
4
>>> 

l had an issue when l run this code in jupyter notebook l was having zeros as the answer which should not be the case. since the uniques are the local scope
letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghljklmnopqrstuvwxyz”
def unique_english_letters(word):
uniques = 0
for letter in letters:
if word in letter:
uniques +=1
return uniques

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghljklmnopqrstuvwxyz" def unique_english_letters(word): uniques = 0 for letter in letters: if word in letter: uniques +=1 return uniques print(unique_english_letters("mississippi")) print(unique_english_letters("Mango"))

print(unique_english_letters(“mississippi”))
print(unique_english_letters(“Mango”))
I don’t know what l should do l need help

No panic, help is on the way. Can you still edit your post enough to format it? That way we can see the indentation.

What am I doing wrong here:

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # Write your unique_english_letters function here: def unique_english_letters(word): uniques = 0 for letter in word: if letter in letters: uniques += 1 letters = letters.replace(letter, "") return uniques # Uncomment these function calls to test your function: print(unique_english_letters("mississippi")) # should print 4 print(unique_english_letters("Apple")) # should print 4