Help please pyglatin


Hi everyone! I made my code for Pyglatin, but when im going to test it, it shows the word without the first letter and without “ay” for example:
enter a word: hola
the result will be: ola.

here’s my 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
new_word = new_word [1:len(original)]

print new_word
print ‘empty’

Thank you for your help!


that is right, part of the translation


new_word = new_word [1:len(original)]

you use len(original), which means "ay"/pyg gets cut off again


Thank you Stetim94 i did the following: new_word = new_word [1:len(new_word)] instead of len(original) i had put new_word. After this works perfectly. :slight_smile:


Do you understand why this change is important and how this change influences the code?


No, I don’t understand.


original contains the original user input (lets go with your example of 'hola'), then we add the first letter ('h' in this case) and 'ay' and store this result in a variable named new_word

so now we have original (value: 'hola') and new_word (value: 'holahay)

now we want to remove the first letter, for this we use a (string) slice. But if you use len(original) as stop value for the slice, you also cut off everything you just added.

as a programmer, understanding is very important, you could have indicated here:

that you made a change, but do not understand the change

i gave you a second change, but here:

you don’t come forward with what you don’t understand. It would have been better if you did indicate what you do or do not understand.


stetim94 . Thank you for your help. what i understand is thenew_word variable which includes the word, first letter and pyg.

what i dont understand is when i write new_word = new_word [1: len (new_word)]. how does [1: len (new_word)] works? , i think this is the key to get the translator to work. is that right? Thanks


if you scroll down a little bit, the docs explain about slicing.


Hi Stetim 94. I check the url you sent me, right now i understand how slicing works but i don’t understand how the line of this code:

new_word = new_word [1:len(new_word)] will create the final result.

what i get is that new_word = new_word, which is word + first + pyg.

So word = original input

first= word [0]

and pyg = ‘ay’

but how the result will be out of this. Thank you


if we want to translate hola, the final translation is: olahay

so we copy the first letter and add the first letter + ay to the end of original string: holahay

so this is the intermediate form of our translation, but because we copied the first letter, now we want to get rid of it, this is where slicing comes into play. We slice of the first letter

strings are zero-indexed based, the first index on the string is 0, so by using 1 as start value for slicing, we remove the first letter

len(new_word) is just there to explicitly state the stop value.


ok perfect! now it makes sense. Thank you Stetim94 for your collaboration :slight_smile:


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