Reputation: 311
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:
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.
Upvotes: 1
Views: 3189
Reputation: 6581
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
else:
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_word
in 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:
ellohay
Upvotes: 2
Reputation: 10951
This is wrong: new_word = [1:len(new_word)]
This is right: new_word = new_word[1:len(new_word)]
Another thing, if you want to slice starting from i to the end of your list, you can do:
new_word = new_word[1:]
Upvotes: 2
Reputation: 293
You use square brackets for slicing, but you have to specify the thing you are slicing.
Just like you can access specific elements of lists, strings and the like, you can also access sublists and substrings. Like so:
a = "this is a long string"
a[0] == "t"
a[0:4] == "this"
a[1:len(a)] == "his is a long string"
You're getting the SyntaxError
because you didn't specify the thing you are slicing.
Upvotes: 1
Reputation: 28606
You need to take a slice of new_word
, not a slice of nothing.
Upvotes: 0