pyg = 'ay' new_word = [1:len(new_word)] original = raw_input('Enter a word:') if len(original) > 0 and original.isalpha(): word = original.lower() first = word print original else: print 'empty'
This line should be inside the if code block, under the line,
first = word
However, there is a line missing that will also come before,
new_word = word + first + pyg
Lastly, your slice needs an object
x = x[1:len(x)]
I don't know if mtf's suggestions are correct, but I think I found a bit simpler way to do it:
First, like mtf said, move this line
new_word = [1:len(new_word)] down beneath
first = word
But then, don't use the line
new_word = [1:len(new_word)]
This line is overly complicated.
Instead, change this part to ''' new_word = word[1:] ''' #Note that ''' word[1:] ''' will include the first index of word to the last index of word, whatever length the word is.
Bu also.... when creating ''' new_word, ''' you also need to concatenate word[1:] with the first letter of 'word' AND the 'ay'
So this line should be...
new_word = word[1:] + first + pyg
Finally, you don't want to print original. You want this instead:
Not if we follow the instructions explicitly. It should be as shown earlier. The slice is the second last step, before printing new_word.
I tried your suggestions and couldn't get them to work, perhaps because I misinterpreted something that you wrote.
But also, did you look at the "Advanced tip?"
When slicing until the end of the string, instead of providing len(new_word), you can also not supply the second index:
my_string = "Python" my_string[1:] # "ython"
Based on this suggestion, I thought it was OK to take an approach that deviated from the instructions.
I wonder if you could paste your entire code, verbatim?
I'd like to see how you got it to work according to the instructions because I couldn't get it to work.
Advanced tips are useful once we understand the concepts. To my mind that means completing the exercise as instructed, passing, then modifying.
first = word new_word = word + first + pyg new_word = new_word[1:len(new_word)] print new_word
Seeing the slice with both indices demonstrates,
The advanced slice defaults to
end (len(x)) when not specified. It pays to have this clear in one's mind going forward.
I agree with you about Advanced Tips and how we should complete the exercise as instructed. I couldn't get the code to work as instructed, but now I get it after seeing your code. Thanks!
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.