pythonrecursion

Recursion function for counting the number of digits in a number?


So I know that this is something simple that can be done without a recursion function but I need to know the backside to this as I can't seem to figure out how to write this using recursion. im using this so far

n = int(raw_input("What is n? "))
def digit(n):
    if n< 10:
        return 1
    else:
        new = n/10
        print 1 + digit(new/10)
        return 1 + digit(new/10)

digit(n)

Now if I type in a number such as 33 then it outputs 2 but if I do a longer number then it doesn't print it properly and I was unsure as to what exactly it wrong with it.


Solution

  •   #!/usr/bin/python
    
      n = int(raw_input("What is n? "))
    
      def digit(n):
          if n < 10:
              return 1
          else:
              return 1 + digit(n/10)
    
      print digit(n)