Where in my code do I make this new slice of new_word?

Question

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.

10 Likes

2 posts were split to a new topic: This Code Seems Counter-Intuitive

in the terminal for Pyglatin
i type Love
and it shows velay
how it’s executed

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

Why can’t I declare the three variables we declared inside the if statement outside of it? What would happen?

technically you could, but why would you? You only want to do these steps when the user input is valid (which the if condition is checking)

I don’t understand the concept of slicing. I will show you :

s = ‘Charlie’

print s[0]
print s[1:4]

When you print this it prints :

C

har

Why doesn’t it print

C
and then

harl
Because the 1 to 4 letters in Charlie are harl

Plz help :unamused:

Review how slicing works: given str[x:y], x is the index of the first character of the slice, and y is one greater than the final index of the slice

That way str[x:len(str)] will work the way you want.

5 Likes

the exercise is

“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 1st index is the 1st index, if it says first letter, then yes, the index would have been zero

i disagree.

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

well, given counting is from zero, the 0st index would be the first element, the 1st index is the second element.

LOL.

no such thing as 0st index.

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.

1 Like

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! :slight_smile:

2 Likes

You can always come to the forum to ask your question? Then we can help you in steps, then you don’t have to peak at the solution :wink:

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:

new_word = word[1:len(word)] + first + pyg

3 Likes

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?

1 Like

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.

thank you for the clarification. But it feels counter-intuitive