Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line. | Sololearn: Learn to code for FREE!

+4

# Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.

I have tried lot to reverse the input of 'PYTHON' but I didn't get the output. I also tried the hint given: spell() but it doesn't work out. Can someone help me.

2/19/2021 7:09:38 AM

Niviedha shekor

+10

def spell(txt): #your code goes here if txt=="": return txt else: print(txt[len(txt)-1]) return spell(txt[0:len(txt)-1]) txt = input() print(spell(txt)) This code is correct for spelling backwards

+7

def reverse(text): n = len(text) if n: print(text[-1]) if 1 < n: reverse(text[:-1]) reverse('PYTHON')

+2

Thanks for your answer dude. But I didn't get the answer. It was given *Complete the recursive spell() function to produce the expected result*

+1

def spell(txt): x = len(txt) for a in range(0,x): print(txt[x-1]) x-=1 txt = input() spell(txt)

+1

But all you have to do it with recursion where is it?

+1

def spell(txt): #your code goes here if txt == "": return txt else: return txt[::-1] txt = input() print(spell(txt))

+1

def spell(txt): #your code goes here if txt=="": return else: print(txt[-1]) return spell(txt[:-1]) txt = input() spell(txt)

+1

def spell(txt): #your code goes here while len(txt) > 0: print(txt[-1]) return spell(txt[:-1]) txt = input() spell(txt)

+1

def spell(txt): #your code goes here if len(txt)==0: return 1 else: print(txt[(len(txt)-1)]) return spell(txt[0:(len(txt)-1)]) txt = input() spell(txt)

+1

def spell(txt): #your code goes here if txt == "": return txt else: print(txt[len(txt) - 1]) return spell(txt[0:len(txt) - 1]) txt = input() spell(txt)

0

just change 'reverse' by 'spell'

0

def spell(txt): #your code goes here x = len(txt) for a in range(0,x): print(txt[x-1]) x-=1 txt = input() spell(txt)

0

#Answer this question def spell(t): X = [] for i in t: X.append(i) r = len(X) -1 while r>-1: print(X[r]) r -= 1 txt = input() spell(txt)

0

def spell(txt): output = “” for i in range(len(txt)): output += txt[(len(txt)-i)-1] + “\n” print(output) An easier way to go about it. This method appends the last element of the word to the output string also adding a new line space. Achieves same output!

0

def spell(txt): #your code goes here testo = [] for x in txt: testo.append(x) for y in testo[::-1]: print(y) txt = input() spell(txt)

0

def spell(txt): for x in range(len(txt)-1,-1,-1): yield txt[i] txt = input() for i in spell(txt): print(i)

0

Great

0

Very simple print last character and if character empty we have to end func all by returning empty string def spell(txt): if txt=="": return txt print(txt[-1]) return spell(txt[0:-1]) txt = input() spell(txt)

0

Shortest code with recursion : def spell(txt): if len(txt)==1: print(txt) else: print(txt[-1] spell(txt[:-1]) txt = input() spell(txt)

0

Pedro Lozano clear and nice , also you can do it without return .