PygLatin translator question


#1

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(new_word)]
    print new_word
else:
    print 'empty'

This little program does not translate more than one word. Is there any way to change it? I thought it was possible to do using .split method and then a for loop. Is my thinking right?


#2

Yes, you could do it that way. However, it might not pass Codecademy's submission correctness test (SCT), because it will be different from what the instructions stipulate.

Give it a try, and post your code, if you have any questions about it.


#3

yea, your thinking is right. Coding this would be a good challenge for you.

one hint, original.isalpha(), spaces are not alphanumeric. Just so you know

for the rest i agree with appylpye, if you get stuck somewhere or have a question you can post it here and we can help you :slight_smile:


#4

original = raw_input('Translate: ')
sentence = original.split(' ')
for i in sentence:
    pyg = 'ay'
    first = i[0]
    i = i[1:len(i)]
    sentence_list = map(lambda i: i + first + pyg, sentence)
#[i.lower() for i in sentence]
#sentence_list = [ i.replace(i, i + first + pyg) for i in sentence if first = i[0] and i = i[1:len(i)]]
new_sentence = ''.join(sentence_list)
print new_sentence

I am trying to understand where it goes wrong. I tried to break the code into parts and both 'first' and 'i' worked but not all at once. I also added the list comprehension as a comment because it's clearly wrong and I'd love to know why.


#5

why use such complicated code, or do you have more programming experience?

i would simply create a empty list, and append() each translated word to this list


#6

You have this within the loop, when it only needs to execute once ...

pyg = 'ay'

The simplest way to process the user's input might be to do it as @stetim94 has suggested above.

If you opt for a list comprehension instead, you can also simply the code quite a bit. You would need to get rid of the for loop, since it would become unnecessary, and revise this comprehension, which cannot work ...

[ i.replace(i, i + first + pyg) for i in sentence if first = i[0] and i = i[1:len(i)]]

After the if, in the above, where only conditions should go, you used the = operator instead of == . If those were actually intended as assignments, you could refactor them into an expression to replace the named variables that you have in the for loop, and then place that expression in the list comprehension prior to the for keyword.

Whichever direction you choose to take, please post your revised code if you have any questions, or if you just want to show us the finished product.


#7

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