10.) Ending Up How To Slice Help!


#1

I keep getting when I put in a word like brad Oops, try again. It looks like new_word contains "bradbay" instead of "radbay"! How can I fix this?


pyg = 'ay'

original = raw_input('Enter a word:')

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


Ending Up Section ("New_word equal to the slice"?)
Ending Up
#3

did you figure it out?


#4

Looks good to me....did they want you to print new_word though?


#5

yeah that was that problem now I have a different problem in the lesson. xD


#6

So I figured it out I needed to put print before that ugh yeah rookie mistake however, now I have a new problem
Oops, try again. It looks like you printed the correct translation of "pig", but make sure to set new_word equal to the slice as well. And I have tried a number of combonations but I can not find the right one to get new_word to equal the slice...

pyg = 'ay'

original = raw_input('Enter a word:')

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

#7

before you print new_word, you should set it to the slice:

new_word = word + first + pyg
# set new_word to the sliced section you need here
# print new_word here

#8

Ahhh thank you very much! And uh did you edit my post? lol got a notification you did but I did not even know that was possible haha


#9

I had exactly the same issue. I figured it out bust still I do not understand why I cannot write it like that

pyg = 'ay'
original = raw_input('Enter a word:')
word = original.lower()
first =word[0]
new_word =word+first+pyg
new_word =word[1:]
if len(original) > 0 and original.isalpha():
    print (new_word)
else:
    print 'empty'

Isn't suppose the print command to grab the latest value of new_word?


#10

This all:

Should be under the if statement as well, because the commands should only be done if the user's input meets the criteria. Assuming your indents are correct, that should pass you.


#11

@itqaservhotmail.com, this line:

new_word =word[1:]

you should take a slice of new_word, which contained the pyglatin version. word still contains something outdated

@chipjumper36731, brackets are valid for printing in python2, in python3 they are mandatory, for the rest good answer, some of the things indeed need to be nested inside the if clause.


#12

Do you mean parentheses? And I don't know what version of Python Codecademy functions on but putting it in parentheses gives me an error.


#13

parentheses or brackets, does it matter? I think codecademy uses python 2.7.3


#14

Yes, it is 2.7.3 :slight_smile:

I don't like using parentheses for printing in Python 2, because they change the behaviour (Python treats arguments as a tuple):

print "Hello,", "Maciej", "Wiercioch"
=> Hello, Maciej Wiercioch

print("Hello,", "Maciej", "Wiercioch")
=> ('Hello,', 'Maciej', 'Wiercioch')

So it's not true that they are optional, they are not treated as a part of the print statement. You simply define the tuple and print it out.


#15

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