pythonhack
pythonhack

Reputation: 69

Print within Recursion function Python3

the point of my recursion function is to print integers in reverse order. def rDisp(s): n=str(s) if n == "": return n else: return rDisp(n[1:]) + n[0]

def main():
    number=(int(input("Enter a number :")))
    rDisp(num)

main()

If within the main function I implement print(reverseDisplay(number)), it works, however, for the purpose of this code, I want the reverseDisplay function to do the printing. How would I go about implementing the print function into that block of code.

Thanks!

Upvotes: 0

Views: 335

Answers (2)

pythonhack
pythonhack

Reputation: 69

Just got it

def reverseDisplay(s):
    n=str(s)
    if n == "":
        return n
    else:
        reverseDisplay(n[1:])
        b=n[0]
        print(b,end='')

Upvotes: 0

Lennart Regebro
Lennart Regebro

Reputation: 172209

Untested code:

def reversePrint(s):
    if not s: 
        return
    print(s[-1])
    reversePrint(s[:-1])

def main():
    number=input("Enter a number :")
    reversePrint(number)

main()

Upvotes: 1

Related Questions