- 1

Spelling Backwards in python

Hi i was doing a code project of the intermediate python course and i have already solved it but my question is: Is there a way to print the text backwards without changing it? like printing it as the function runs =================================================================== NEXT IS THE CODE PROJECT STATEMENT: Spelling Backwards Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line. Sample Input HELLO Sample Output O L L E H Complete the recursive spell() function to produce the expected result. =================================================================== MY SOLUTION: def spell(txt): #your code goes here def reverse(txt): if len(txt) == 0: return txt else: return reverse(txt[1:]) + txt[0] txt = reverse(txt) for i in txt: print(i) return txt = input() spell(txt) =================================================================== I would like to know how you would have solved it or other ways to solve that problem

28th May 2022, 6:00 PM
Kevin Steling
Kevin Steling - avatar
2 Antworten
+ 1
No loop or recursion required: def reverse(txt): return txt[::-1] ----- Short version of your program: print(*list(input()[::-1]), sep='\n') Input: HELLO Output: O L L E H
28th May 2022, 7:15 PM
Brian
Brian - avatar
0
for i in input()[::-1]: print(i)
28th May 2022, 6:05 PM
NEZ
NEZ - avatar