Pyglatin set new_word equal to the slice as well


#1


Hello guys, Im not seeing the problem of my error? Can you maybe find the error/ tell me what's wrong
or in case- help me understand the problem?


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


Enter a word: doei
oeiday
None

Oops, try again. It looks like you printed the correct translation of "doei", but make sure to set new_word equal to the slice as well.


I dont know how it should be


#Replace this line with your code. 
pyg = 'ay'

original = raw_input('Enter a word:')

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


#2

Hi,

You have to set new_word to the thing that gets printed to the console:

new_word= word+first+pyg
new_word = new_word[1:]
print new_word

instead of

new_word= word+first+pyg
print new_word[1:]

#4

I don't think the blank lines make a difference. Both versions will fail on empty input and pass for valid input.


#5

Thank you very much eveat it works now


#6

thanks for the comment Ill try my best to think about the blank spaces :))


#8

Blank lines have ZERO effect on how our code runs. Repeat, ZERO EFFECT.


#9

I think he's trying to say that it's messing him up.. Not his code :mask:


#10

why do we have to set new word to a variable of new_word[1:]
even though new_word was not set to that variable the code works somehow
can you explain a little bit about this?


#11

Aside

There are countless ways we can write this program. The instructions for the exercise break it down to a step by step procedure, even as the code is built. We started out with a basic condtional just to set the framework...

    pyg = 'ay'
    original = raw_imput("Enter a word: ")
    if len(original) > 0:
        print original    # temporary
    else:
        print "empty"

Further to this, we next added text validation to the conditional.

if len(original) > 0 and original.isalpha():

The last steps replace the print original statement with the procedural code. Again, step by step creating the new_word string.

word = original.lower()

This is sensible since it preserves the original. Only the working value is mutated. A couple more steps and the new string will be built in the raw.

    first = word[0]               # intermediate step
    new_word = word + first + pyg

Because we have defined new_word we can now write exactly what the instructions ask for...

new_word = new_word[1:len(new_word)]

The above notwithstanding we can refactor the code, after we follow the instructions and pass.

new_word = new_word[1:]

is an identical slice. Notice that the lesson expounds upon this fact by showing the similarity. If we only ever saw the latter form, we might never fully comprehend what value is implicitly included in the short form slice syntax. The joys or introductory lessons.

There is much joy in paring code down to its simplest form, once we know the logic and procedures are correct.

def pyg_latin(term):
    return ("%s%say" % (term[1:],term[0])).lower()

print pyg_latin("Codecademy")            # odecademycay

This probably won't pass the exercise, but it definitely works as expected.


#12

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