10. Ending up...with invalid syntax


#1

Hello here's what I have so far...
pyg = 'ay'

original = raw_input('Enter a word:')

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

Here's the response I get:
File "python", line 12
new_word = [1:len(new_word)]
^
SyntaxError: invalid syntax

It seems to not like my colon. (I did eat some spicy food so I can understand) But seriously, what am I missing? Thanks


#2

So I've read through some of the other Q&A topics on 10. Some have suggested that the new_word needs to be within the if: statement. This doesn't make logical sense to me. I feel like it shouldn't have to. Once the word is evaluated as actually being a word everything should follow through. Right?
To test my theory I moved the line in question, 12, and the one prior to it, 11, into the if: statement.
I got the same results, a syntax error with the carrot pointing at the : in the new_word = [1:len(new_word)] line. Thanks


#3
new_word = [1:len(new_word)]

Should be

new_word = new_word[1:len(new_word)]

#4

i am having a hard time figuring out why this does not work too. did you ever get it??


#5

Why the original version did not work? I'd almost say that a better question would be "What makes you think that would have worked?"

new_string = old_string[1:5]

This means " assign index 1 (inclusive) to index 5 (not inclusive) of old_string to new_string.

new_string = [1:5]

This does not really mean anything at all in python, so it will throw a "SyntaxError"

new_word = new_word[1:len(new_word)]

What this line does is assigning new_word to all it's letters except the first one. It's actually a bit of an awkward way of writing it. Writing it like the following does the same thing and looks nicer:

new_word = new_word[1:]

#6

TY very much for your help


#7

Thanks, I see and understand the error I made. Totally missed that in the demo on the exercise page.


#8

what was the error you made i still cant seem to get it


#9

awesome! I was stucked at the same point. Thank you


#10

This worked for me, but I'm having a little trouble wrapping my mind around what is happening with it. Could you explain what is happening here?


#11

original = raw_input("Enter a Word:-").lower()
if len(original) > 0 and original.isalpha():
print(original)
word = original
first = word[0]
new_word = word + first + "py"
new_word = new_word[1:len(new_word)]
print(new_word)
else:
print("Empty")

Hay, check out this piece, I thought u will b missing some indentation at if...: or at else:


#12

mine worked fine, probably u might use the correct indentation


#15

I wrote it like this:
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:len(word)] + first + pyg
print new_word
else:
print 'empty'

Hope this helps; I don't think it's the conventional way to do it but it works!