Def count_unique(word,letter ): y =[] if len(letter) == 0: for x in word : if x not in y: y.append(x) return len(y) z = [] else: for x in word : if x == letter: z.append(x) return len(z)

why my else statement throwing syntax error

Hey there @elafansari, and welcome to the Codecademy forums :grinning:

Python as a coding language relies heavily on indentation to designate code blocks, as such since you included your code as your title, no one can accurately test it.

Please post your code formatted by pressing this button:

And then copy/paste your code between the two rows of backticks:


Based on an educated guess since you are having your else throw the error, I would say your code probably looks somewhat like this:

def count_unique(word,letter): 
  y =[] 
  if len(letter) == 0: 
    for x in word : 
      if x not in y: y.append(x) 
    return len(y) 
  z = []
  else: 
    for x in word : 
      if x == letter: 
        z.append(x) 
    return len(z)

In this case you would be getting the error because there is a line of code directly between the if and the else.

An else must come right after the if, on its own indentation:

x = True

if x:
  print(True)
else:
  print(False)

Note that there is no space between theif and the else along its indentation. If there is space than there will be a SyntaxError since it is not part of an if:

x = True
if x:
  print(True)
random_variable_interrupting_an_if_block = "I foresee a SyntaxError"
else:
  print(False)
3 Likes