-1

I am writing a code in python 3 to display number of digits of a given number. The code is given below:

count=0.
def fun(n):
     global count_e
         if  n<0 :
             count_e+=1
         else:
             res=fun(N/10)
             count_e+=1
N=int(input())
fun(N)
print (count_e)

I am getting indentation error. I do not know how to proceed.

I tried to explain issue with my python code. I expect a reply with a suggestion.

Markus B
  • 83
  • 1
  • 10
Ezhil
  • 13
  • 2
  • Where does the error say the problem is? – Scott Hunter Feb 21 '23 at 13:55
  • Unindent your *if* block. Also you'll need to define *count_e*. Then you'll need to think about negative numbers and the differences between float and int - e.g., what should the answer be if *n == 12.5* ? Have you considered the implications of recursion here? – DarkKnight Feb 21 '23 at 14:01

2 Answers2

0

This might be what you are looking for?

count=0.
def fun(n):
    global count_e
    if  n<0:
        count_e+=1
    else:
        res=fun(N/10)
        count_e+=1
N=int(input())
fun(5)
print(count_e)

Moving the if statement inwards to be at the same indentation of global count_e

Markus B
  • 83
  • 1
  • 10
  • 1
    Please avoid answering typo-related questions. Instead, flag them as such so they get closed. If you want to be helpful, you can leave a comment indicating what the typo is, although in this case the question is even a common duplicate with many answers there – Tomerikoo Feb 21 '23 at 14:15
-1

You should avoid the use of global. Here is a working function for what you need:

def get_number_of_digits(int_number):
    int_number = abs(int_number)

    number_digits = 1
    while int_number >= 10:
        number_digits += 1
        int_number /= 10

    return number_digits
  • The question here is not really ***how*** to count the number of digits. It's how to solve the error in the code (which is not even posted...) – Tomerikoo Feb 21 '23 at 14:14