Pig Latin Translator breaks with the else statement for some reason

Link: https://www.codecademy.com/courses/learn-python/lessons/pyglatin/exercises/testing-testing-is-this-thing-on-?action=resume

I was following along with this course on creating the Pig Latin translator and as I am following along, suddenly my if/else statement stopped working. It kept giving me an error on the else line, saying:

File “python”, line 13
else: print “ok”
SyntaxError: invalid syntax

Here is my code:

pyg = 'ay'
s = "Charlie"

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
	word = original.lower()
	first = word[0]
  	new_word = word + first + pyg
    	new_word = new_word[1:]
print new_word

else: print "ok"

It works if I take out the else statement. I dont understand why the else part of this is messing this up, when I was able to use the else portion of this before I had to add all the extra variables within the “if” statement".

1 Like

else can only be after an if or an elif:

if condition:
   print "code in the if block"
  print "condition is false, do something else"

given how you nested print new_word, else is not after an the if statement.

1 Like