Where in my code do I make this new slice of new_word?
Answer
A common issue here is trying to replace what we already had for new_word, where we concatenated word, first, and pyg. For this exercise we should change new_word again, but by assigning a value over it using the existing version of itself. Take a look at the example below for a better idea:
my_word = first + middle + suffix
my_word = my_word[index_to_slice_goes_here]
Otherwise, we’d be writing new_word = new_word[...] without having defined new_word yet, which will give you an error that reads: NameError: ‘new_word’ is not defined because we’re trying to use it to define itself, which is invalid.
There is space between I love you so is.alpha doest not count the space so it would not execute so you could use or insted of and . Even you could add the ASCII code of the characters
“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.”
the 1st index is 0 so how it became a 1 ?? the instruction should be reworded to be “… Use '[0:len(new_word)]…” OR mention “…2nd index…” instead of “…1st index…”.
the first index of any array is always zero. for example, the array array1 = (1, 2, 3, 4) what is the value of the first index? starting from zero, the value of the first index is “1”.
All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1
in the problem description, if what is indended is the second member of the array then it should have been referred to as the second element.
but second element is at index position 1, aka the 1st index. You have to bridge a gap here, that human counting starts at 1 while indexes start counting at 0.
we can endless argue about this, but it doesn’t matter. You need to understand what the exercise asks of you, and how to achieve this.
Just wanted to point out that the instructions for for step 10 are a little unclear. Not only is it tricky that we need to create a new variable, but we also have to omit the print statement. No way I would have been able to figure that out without peaking at the solution. It would be nice if the instructions were more thorough! I prefer not to look at the solutions, but you leave me no choice!
I keep getting a syntax error when slicing, with the syntax error pointing to the colon:
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = [1:len(word)] + first + pyg
print new_word
else:
print ‘empty’
File “python”, line 8
new_word = [1:len(word)] + first + pyg
SyntaxError: invalid syntax
EDIT: nevermind, I realized I wasn’t calling the variable before the slice. Corrected by changing to:
how does this work ‘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.’, when the new_word is just being created and also it includes + first + pyg before the it is know how many letters it will have?
len() will count the number of characters which are currently in the string. The addition to the string has already occurred, so will be included in the count.