Pyglatin question on slice


#1

My code is:

pyg = ‘ay’

original = raw_input(‘Enter a word:’)

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

On the bolded line, the code uses a slice statement, exactly as the tutorial specifies. This does work.
My question:
How can you slice new_word when it has no value until the assignment executes?


#2

The above is taking a slice of word, but it should follow the line that creates new_word.

new_word = word + first + pyg
# set new_word to a slice on `new_word`
# print the new word

#3

The code I posted does give the desired results and seems to follow the directions, which read:

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.


#4

As mentioned earlier, your code does not follow the instructions. Construct the new_word string first, then take a slice of it and assign it back onto that variable.


#5

I believe I followed the instructions but whether I did or not is irrelevant. When I ran the program the results were as desired. My question was how could this be when the line referenced an undefined variable. I spoke to someone knowledgeable who said it was possible that an undefined variable string, such as my new_word, has a default len of whatever is the max. Given that len, the result is now understandable.


#6

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