Help please pyglatin


#1

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
else:
print ‘empty’

Thank you for your help!


#2

that is right, part of the translation

here:

new_word = new_word [1:len(original)]

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


#3

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:


#4

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


#5

No, I don’t understand.


#6

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.


#7

Hi
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


#8

https://docs.python.org/2/tutorial/introduction.html#strings

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


#9

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


#10

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.


#11

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


#12

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