Why is this syntax invalid?


#1

I am in the conditionals section step 11

The instructions that they are saying at the very top do not make sense to me.

“Well done! However, now we have the first letter showing up both at the beginning and near the end.”

What does this even mean? the lesson doesn’t do that at all.

But the kicker is this:

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.

That is easy enough to understand, but the console says [1:len(new_word)] is not valid:

File “python”, line 10
new_word = [1:len(new_word)]
^
SyntaxError: invalid syntax.

I am not sure if this is a bug or the instructions are wrong but this is what I am getting.

Any ideas?


#2

I have to admit the instructions are a bit vague. But you need to make it a variable.

The first variable you should’ve made is:

new_word =  word + first + pyg

That was in the previous lesson, next you need to fit in [1:len(new_word)] into the new_word variable. Now how do you think you can achieve that?


#3

if we want to take a slice of a string, we have the following general syntax:

"string"[start:stop]

in your case, the string of which you want to take a slice is missing, so python doesn’t know what to take a slice off, so it throws an error


#4

The instructions tell us to do so like the OP says though

1. 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.


#5

If you review the syntax for taking a slice of an object, the current exercise will be less confusing.

See:

Notice that prior to the square brackets, you must specify the object that is to be sliced, as @stetim94 has shown above.


#6

Hi,
I did have “new_word = word + first + pyg”, but that was not the issue. I searched this site and the instructions have the syntax wrong. It should be:

new_word[1:len(new_word)]

UP to step 9, everything was fine, then I got this:

“Well done! However, now we have the first letter showing up both at the beginning and near the end.”

I not sure what they are trying to say…it’s like they jumped over a step. I have had all kinds of connectivity issues and the lessons do skip ahead for the past 2 weeks or so, but I don’t think that is it this time as I have learned how to deal with this by resetting the lessons each day.

The other thing is I don’t think they tell you to print out to the console to test, but once I found the correct syntax and added the print statement it started working.

Anyway, I am good for now but I wish they would have explained things better at step 10.

Thanks!


#7

Thanks…yes that helps!


#8

Remember to assign this to new_word.


#9

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