Lyubo Lyubo - 1 year ago 298
Python Question

SyntaxError: Invalid syntax in a codecademy program

in the PygLatin 10/11 exercise on the Codecademy Python course, i have to do the following:

Set new_word equal to the slice from the 1st index all the way to the
end of new_word. Use [1:len(new_word)] to do this.

And I receive the following error:

File "python", line 9
new_word = [1:len(new_word)]
SyntaxError: invalid syntax

And this is the code:

File "python", line 9
new_word = [1:len(new_word)]
SyntaxError: invalid syntax

Here you will find screenshots which might be of help:

enter image description here

This might be a very stupid question, but I really want to understand where I am wrong with this, soIi don't make the same mistake in the future. Thanks for the help in advance.

Answer Source

This is how the code should be:

pyg = 'ay'

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
    print 'empty'

Let's say the input is Hello. This line: new_word = word + first +pyg will give you hellohay but what you need is ellohay so you need to slice the new_wordin order to lose the first h which is new_word[0] so, new_word should start with e, which is new_word[1] this is why you need this line: new_word = new_word[1:]

The output is:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download