Help needed with finding a unique string challenge

here’s the challenge: link

My code is passing only 3 out of 5 tests according to codeacademy, but I’m not sure what to do. here’s the code:

def unique_characters(string):
  for i in string:
    for j in string:
      if i == j:
        return False
      else:
        return True

Hello @rohanjagadeesan20214, welcome to the forums!
Your code is failing those tests because of the way the loops are working. The first loop goes through all of the characters of the string (let’s say “apple”). The next loop then starts at the start of the list, and loops through all of the letters. That loop is therefore bound to find “duplicate” letters

word = "aple"
loop one (first letter is "a"):
  loop two (runs through all the letters of apple-first letter is "a"):
   if "a" == "a":
    return False

As you can see above, even if the word has no duplicate letters, the loops will always find “duplicate” letters.

ahh that’s what it was. Thanks! I didn’t think to test the code using a word with unique strings.

1 Like