Code works but doesn't translate to Pig Latin


#1
pyg = 'ay'
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]

original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
  print new_word
else:
  print 'empty'

#2

There is some difficulty with the organization of the code. It is out of order (operations are not taking place at the right time).

Step one: Define the global.

pyg = "ay"

Step two: Get the user input and store it:

original = raw_input("Enter a word: ")

Step three: Validate the input:

if len(original) and original.isalpha():

Inside the IF is where all the rest of the code goes, except for,

else:
    print "empty"

#3

That means its an internal problem, with codecademy?


#4

No problem, unless your corrected code doesn’t run.


#5

So now I have

pyg = 'ay'
original = raw_input('Enter a word: ')
if len(original) > 0 and original.isalpha():
  print new_word
else:
  print 'empty'
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]

and the user input is working but I still get no pig latin and i get an error saying

Traceback (most recent call last):
  File "python", line 4, in <module>
NameError: name 'new_word' is not defined

#6

Replace this,

with this,


#7

It works! The problem I was facing was that I need to remove else: print 'empty'
Here is my final code for those who want it

pyg = 'ay'
#Asks for the user to input a word
original = raw_input('Enter a word: ')
#Says if the word has more than zero letters and has no numbers then execute block
if len(original) > 0 and original.isalpha():
  #A bunch of variable stuff lol idk
 word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
#Says to log the translated word into the console
print new_word

#8

Odd that it finally passed, under those conditions, but the lesson checker is not exhaustive and usually only looks for a specific thing to be present, and once found passes the lesson.

The else clause is important to the code and should stay at the end, as described earlier. The order of your statements is critical. We’re talking to a computer and every step must be spelled out and in sequence.

Your last edit shows a problem… There is no indent on the last five lines (comment included in count). Indents should be four spaces each line for every line below the if.

else:
    print "empty"

gets tacked on after the indented print new_word line. It should line up with the if (no indentation).


#9

So that’s why it was telling me there was an indention error. I’ll see if I can’t put that bit of code back on.


#10

To Python, indentation is critical since it is part of the syntax that tells the interpreter it is working with a block. Blocks have internal scope and definition that depends heavily on the signals received from indentations in our program.

What {} is to C, Java, ECMAScript (JS), etc, the indent is to Python.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.