word = str(input()) word.reverse(word) word.insert('ay') print (word) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

word = str(input()) word.reverse(word) word.insert('ay') print (word)

What is wrong with this code??

3rd Jul 2021, 10:16 PM
Gee Pebbles
Gee Pebbles - avatar
17 Answers
+ 3
If you try to solve pig latin challenge you should read the description again. Take the first letter and put it at the end. Add "ay" hello -> ellohay And you should check if you get only one word or a sentence. Btw: input() returns a string so converting to a string is not necessary.
3rd Jul 2021, 11:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
what should be the output? input: hello output: ollehay or ellohay? also, reverse() is a method working on list and taking no argiments, not strings... you could use reversed() function but it will return a list, not a string ^^ insert() too is a list method, taking index at first argument and element to insert at second argument... not working on strings ;P
3rd Jul 2021, 10:22 PM
visph
visph - avatar
+ 1
print(*(s[1:]+s[0]+"ay" for s in input().split()))
4th Jul 2021, 2:33 PM
visph
visph - avatar
+ 1
Do you think the input will be just one word? What if input is the sentence? String in python acts exactly as same as immutable lists You can refer to any item of the string by its corresponding index number E.g x = "hello brother" print(x[0]) #outputs h, because h in the 0 index (at the beginning of the string) All you need is just creat new string with concatenation of old string's slices in correct way and add "ay" at the end!!!
4th Jul 2021, 3:32 PM
Shadoff
Shadoff - avatar
+ 1
Thanks but Calvin Thomas it didn't work 😔
5th Jul 2021, 5:35 PM
Gee Pebbles
Gee Pebbles - avatar
+ 1
Calvin Thomas check my answer: it's also a oneliner ^^ it's quite shorter than your, and doesn't output a final space (wich could prevent sololearn code coach tests to success ;P)
5th Jul 2021, 6:07 PM
visph
visph - avatar
+ 1
""" Calvin Thomas multiple inter-word spaces such as in " abc def "? but spliting such string with default separator give: ['abc', 'def'] and with explicit " " separator give: ['', '', 'abc', '', '', '', 'def', '', ''] so dealing with multiple space should only use: """ print(*(s[1:]+s[0]+"ay" if s else "" for s in input().split(" ")))
6th Jul 2021, 6:39 AM
visph
visph - avatar
+ 1
Calvin Thomas that sound logical: spliting at spaces will remove spaces and give strings in between ;)
6th Jul 2021, 6:47 AM
visph
visph - avatar
0
Thanks 🙂
4th Jul 2021, 8:59 AM
Gee Pebbles
Gee Pebbles - avatar
0
I tried the correcting it but it didn't work
4th Jul 2021, 12:19 PM
Gee Pebbles
Gee Pebbles - avatar
0
Thanks
4th Jul 2021, 3:10 PM
Gee Pebbles
Gee Pebbles - avatar
0
visph it didn't work
4th Jul 2021, 3:17 PM
Gee Pebbles
Gee Pebbles - avatar
0
Gee Pebbles sorry for the typo, I forgot parenthesis on input() call... corrected: check it again ;)
4th Jul 2021, 3:26 PM
visph
visph - avatar
0
Gee Pebbles Here's a one-liner: [print(a[1:]+a[0]+"ay ", end="") for a in input().split()] # Hope this helps
5th Jul 2021, 4:52 PM
Calvin Thomas
Calvin Thomas - avatar
0
visph Yup, your version is better.
6th Jul 2021, 2:26 AM
Calvin Thomas
Calvin Thomas - avatar
0
visph Here's a slight modification of your code, which can deal with multiple inter-word spaces: print(*("" if s in " " else s[1:] + s[0] + "ay" for s in input().split(" "))) Edit: ..."" if s else...
6th Jul 2021, 6:21 AM
Calvin Thomas
Calvin Thomas - avatar
0
visph Ah, I didn't notice that they're all null strings. Thanks for the suggestion.
6th Jul 2021, 6:43 AM
Calvin Thomas
Calvin Thomas - avatar