Slicing help?


#1



https://www.codecademy.com/courses/python-beginner-2W5v7/1/4?curriculum_id=4f89dab3d788890003000096#


It's telling me that my original variable is slicing the first index of python but it's not adding my pyg variable...?


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:]
    new_word = word + first + pyg
    print original
else:
    print 'empty'

I passed, but something weird happened. It told me I was correct by doing.

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:6] + first + pyg
    print original
else:
    print 'empty'

And what this did was when it asked for my input, I put in "python"
And it printed "python" but the lesson was telling me to remove the 0 indexed of the original variable and to add the pyg variable to the end. Which did not happen in this case.

Imgur

I then solved it again by doing this instead.

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:6] + first + pyg
    print new_word
else:
    print 'empty'
Imgur

And all I did was change the print from print original, to print new_word.
I feel like this isn't what was supposed to happen either... I feel like I skipped over something and missed what this lesson was about.


#2

You did.

When constructing new_word do not slice word, just string them all together:

word + first + pyg

Then slice new_word and use the length of new_word to set the length of the slice (as given in the instructions). The short form is fine, but the auther would like us to see the long form at least this one time. Assign the slice back onto new_word.


#3

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