+ 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))
10 odpowiedzi
+ 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 "
+ 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.
+ 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
+ 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().
+ 2
latin += x.replace(x[0],"") + x[0] + "ay "
👆 👆
+ 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)
0
Solo …Hey i made the corrections but it’s showing that 2 tests are still wrong😭
0
The future is now thanks to science[In a break] ohh okay thank you
0
Nice
0
HELLO hello my name is saidakbar from uzbekistan tashkent city