Ending up


#1



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


It behaves incorrectly as in the exercise allows me to finish if it prints original instead of new_word, despite it being wrong and only the result displaying the original word and not pig latin. So I changed it to print new_word and it worked again with the right results, but I'm confused as if I was to do this in the first place(change original to print new_word).

Also, apparently this works new_word = new_word[1:] though the episode said to have it like [1:len(new_word)], any reason as to why this fixed the code is welcome, thanks in advance.


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
    new_word = new_word[1:]
    print new_word
else:
    print 'empty'


#2

First point:

I think CC is bound to instructions, as It says to slice new_word and reassign it again to it and as It sees you have done it, It lets you pass even printing original string (Point me out if I am wrong, or it can be a bug?)

Second point.

new_word[1:] and new_word[1:len(new_word)], Both of them do same thing.

First one means slice string from index 1 character to the last.

second one means slice string from index 1 to len(new_word) characters (It does slice but till including len(new_word) - 1 index character,as final index are excluded while slicing) as final index of string character is len(new_word) - 1 so it includes character 1 to last character of string new_word


#3

Hi, thanks for the very detailed response. In regards to the second one I appreciate that you went into it, but I'm still unsure as to why the code didn't work when the longer new_word[1:len(new_word)] was typed.


#4

IMAGE DELETED

I have no idea why It does not worked for you, but Its working for me.


#5

I mean I used it without putting = new_word after the =. Why do I need to do this?


#6

That's a mistake.
When we want to re-assign/assign a value to an identifier(variable) we use assignment operator =.
what assignment operators does is ,It saves the value to a memory block.
say..

a = 23
a is an identifier
23 is a value
= an operator that makes connection between them

So assignment operator makes a connection between a and 23.
and whenever we want to use the value 23 ,we will use a ,as it reference our value 23.
Like this..

so..same way

new_word = new_word[1:len(new_word)]


#7

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