10. Ending Up


#1

https://www.codecademy.com/en/courses/python-beginner-2W5v7/1/4?curriculum_id=4f89dab3d788890003000096

I am receiving the message "Oops, try again. Your code did not run to end - see the console window for the error message." The error message is:

  File "python", line 8
    new_word = [1:len(new_word)] + first + pyg
                 ^
SyntaxError: invalid syntax

I'm not sure why because I got the [1:len(new_word)] part right from the instructions.

Here's my code:

pyg = 'ay'

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)] + first + pyg
    print new_word
else:
    print 'empty'

Thanks in advance!


#2

If you're gonna slice some sequence, you'll have to say WHICH sequence you're slicing.

This won't do:

[5:7]

You'll need to do something like:

range(100)[5:7]

#3

@queensferry,
If you have a string
word="Saturday"
you can take a so-called slice of this string
for instance
first=word[0] will be the same as using
first="S"

But you can also get a slice using a =start= and =end= indicator
like
sliced-word= word[1:len(word)] which would give "aturday"
http://stackoverflow.com/questions/509211/explain-pythons-slice-notation?lq=1


#4

@ionatan @leonhard.wettengmx.n Thank you, it's working now!


#5

take this
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:len(new_word)]
print new_word[1:len(new_word)]


#6