- 1
word = str(input()) word.reverse(word) word.insert('ay') print (word)
What is wrong with this code??
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.
+ 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
+ 1
print(*(s[1:]+s[0]+"ay" for s in input().split()))
+ 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!!!
+ 1
Thanks but Calvin Thomas it didn't work 😔
+ 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)
+ 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(" ")))
+ 1
Calvin Thomas that sound logical: spliting at spaces will remove spaces and give strings in between ;)
0
Thanks 🙂
0
I tried the correcting it but it didn't work
0
Thanks
0
visph it didn't work
0
Gee Pebbles sorry for the typo, I forgot parenthesis on input() call... corrected: check it again ;)
0
Gee Pebbles Here's a one-liner:
[print(a[1:]+a[0]+"ay ", end="") for a in input().split()]
# Hope this helps
0
visph Yup, your version is better.
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...
0
visph Ah, I didn't notice that they're all null strings. Thanks for the suggestion.