Home > Software design >  how to count seperate digits of an integer up in python?
how to count seperate digits of an integer up in python?

Time:10-07

The user should input a number and the program has to count all the digits of the number up excluding the last digit.

for example:

input: 12345

n = 1 2 3 4= 10

.

i currently have

n = int(input())

n = n // 10

di = [int(d) for d in str(n)]
    
print(dig)

above code outputs:

(executing file "clock.py") 123 [1, 2]

CodePudding user response:

One more step, that's all.

n = int(input())
n = n // 10
di = sum([int(d) for d in str(n)])
print(di)

Output for 123:

3

CodePudding user response:

I hope, my answer can help you

number = "12345"

convt2arr =  list(number)

result = 0

for x in convt2arr:
    if int(x) < len(convt2arr):
        result  = int(x)

print(result)
  • Related