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

Pigs Latin

Please can anyone tell me what I’m doing wrong?. It’s only showing the last word. word = input() app = word.split() latin = [] for x in app: latin = x.replace(x[0],"") + x[0] +"ay" print("".join(latin))

31st Oct 2022, 8:59 PM
Comfort Agi
Comfort Agi - avatar
10 Answers
+ 5
Comfort Agi , I don't know why some test cases don't like the replacement, so I'd make it simpler: latin += x[1:] + x[0] + "ay "
31st Oct 2022, 10:50 PM
Solo
Solo - avatar
+ 5
Do you know what your code deliver? You can save your code in Sololearns Playground and test with print statement and a few input data what in your code happend and think about a better way.
31st Oct 2022, 9:14 PM
JaScript
JaScript - avatar
+ 3
Cause you are overwriting the Latin every time when you are using the loop so the last one remains at the end of the loop. You can use list.append(value) to append one after another, and ig it should be printed as a sentence so give a gap " " with the join function. https://www.sololearn.com/compiler-playground/cr9mhfkflJaL
31st Oct 2022, 9:11 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 3
If you use the replace() method, beware that it replaces all occurrances of the search string. latin = x.replace(x[0],"") + x[0] +"ay" Example: x = "nanotech" x.replace(x[0], "") looks like "nanotech".replace("n", "") and becomes "aotech" See Solo's answer for how to use string slicing instead of replace().
31st Oct 2022, 11:25 PM
Brian
Brian - avatar
+ 2
latin += x.replace(x[0],"") + x[0] + "ay " 👆 👆
31st Oct 2022, 9:09 PM
Solo
Solo - avatar
+ 2
I modified your code and added some comments. Hope it helps. word = 'nevermind youve got them' wordlist = word.split(' ') # convert word to list of words by using split method piglist = [] # create temporary list for storing words in 'pig latin' for singleword in wordlist: singleword = singleword[1:] + singleword[0] +'ay' # split string by slicing and append first character of it + 'ay' at the end of word piglist.append(singleword) pigstring = " ".join(piglist) # finally create single string from list to match required output print(pigstring)
31st Oct 2022, 9:41 PM
Mateusz Kempa
Mateusz Kempa - avatar
0
Solo …Hey i made the corrections but it’s showing that 2 tests are still wrong😭
31st Oct 2022, 9:32 PM
Comfort Agi
Comfort Agi - avatar
31st Oct 2022, 9:33 PM
Comfort Agi
Comfort Agi - avatar
0
Nice
2nd Nov 2022, 3:50 PM
ASIM FARHEEN ❄️⚡⚡🤳⚡⚡❄️
ASIM FARHEEN ❄️⚡⚡🤳⚡⚡❄️ - avatar
0
HELLO hello my name is saidakbar from uzbekistan tashkent city
2nd Nov 2022, 7:26 PM
Sunnat Narziqulov
Sunnat Narziqulov - avatar