Pig Latin HELP | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Pig Latin HELP

en = input() wl = list(en.split()) for i in wl: a = wl[i] a += a[1] a += ('ay') a = a[i][1:] print(str(wl)) It says a = wl[i] is wrong WHY?

5th May 2020, 5:11 AM
kxrxkt
kxrxkt - avatar
4 Answers
+ 7
en = "Hello World" en.split() would split it by words and return a list with the following elements - ['Hello' , 'World'] So if there's only one word in the input, there will be only one element in that list. So w[1] will give an "index out of range" error. What you are want is to split the word by "letters". For that, Replace the 2nd line with - wl = list(en) And it should work. Tell me if it works for you after trying this.
5th May 2020, 5:37 AM
Nikhil
Nikhil - avatar
+ 2
Ooooh, I got it! I forgot that the first element is [0] but not [1]
5th May 2020, 5:40 AM
kxrxkt
kxrxkt - avatar
+ 2
Here is final code: en = input() wl = en.split() word = 0 for i in wl: wl[word] += wl[word][0] wl[word] += 'ay' wl[word] = wl[word][1:] word += 1 print(' '.join(wl))
5th May 2020, 5:54 AM
kxrxkt
kxrxkt - avatar
+ 2
Oh, haha. Glad you got the answer.
5th May 2020, 7:38 AM
Nikhil
Nikhil - avatar