Am I following the instructions?


#1

Hello! I'm not quite sure if I'm following the instructions. I submitted the following code to the console today:

pyg = 'ay'
print "Welocme to the PygLatin translator!"
original = raw_input('Enter a word:')

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

Then, I got the following error message:
File "python", line 8
new_word = [1len(new_word)]
^
SyntaxError: invalid syntax
The instructions said I should use the function returning an error to "Set new_word equal to the slice from the 1st index all the way to the end of new_word" so I'm not quite sure I'm following the instructions right. Please help!


#2

When you're slicing something, you have to include what that something is - see instructions for an example of the syntax


#3

You should not remove the things (new_word = word + first + pyg) added in the previous lesson: https://www.codecademy.com/en/courses/python-beginner-2W5v7/1/3?curriculum_id=4f89dab3d788890003000096

As for what the instructions say ("Set new_word equal to the slice from the 1st index all the way to the end of new_word"), it could be quite misleading. When slicing, the len(new_word) refers to the length of the variable new_word. However you did not establish the new_word yet, so you should not (or cannot) be finding it.


#4

Does this look right? Because it returns the same error.
`python
pyg = 'ay'
print "Welocme to the PygLatin translator!"
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 = [1:len(new_word)
else:
print "empty"


#5

new_word = word + first + pyg
new_word = [1:len(new_word)

Your 2nd new_word replaces the first one. You should merge both of them together to reach the result asked for. For instance, from Python to ythonpay. You need to make it such that the first letter of the original word is moved to the end of the remaining original word (this is where the slice comes in handy, to find the length of the remaining original word), with an addition of "ay" (contributed by pyg) at the end.

Take note the slice should have opening and closing brackets too. In the 2nd new_word, you have the [ but not the ].


#6

So you're saying I need to do this:

new_word = word + first + pyg + [1:len(new_word)]

#7

Nope. Your new_word would not return Python as ythonpay.
It should be like this:

new_word = addSomethingHere + first + pyg

addSomethingHere should be the remainder of the first word (ython), to make that, you need to slice the original word such that it only shows the remainder of the word from the second letter onwards (word[1:len(word)])

word[1:len(word)] works like this:

fruit = "banana"
new_fruit = fruit[1:len(fruit)] #slices from the second letter of fruit onwards
print new_fruit #prints "anana"

Change fruit to word and you may see some parallels. :slight_smile:


#8

Okay. I changed the script to:

new_word = word + first + pyg + new_word[1:len(new_word)]

and got the error:

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

#9

What will you get if you print out new_word without the new_word[1:len(new_word)]?

Assuming the original word is Python, you will get pythonpay instead of ythonpay.

The problem is the first and last thing you're adding into new_word. The only thing you should be slicing is the original word (word). Try removing the last object, and change the first object with the slice method.


#10

Thanks, it works!! :grinning:


#11

Funny fact , if you'll input only one letter in the console after you use the : new_word = NoMatterWhat + pyg , you'll success the exercise as long as you don't have any further wrong syntax :slight_smile:


#12

The instructions for this lesson were a bit confusing, and clearly I'm not the only one who thought so as well. In a regular compiler there are a few different ways to get the same result, though the only one that I actually got to work for this lesson was:

pyg = 'ay'

original = raw_input('Enter a word:')

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

print new_word


#13

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