#how can correct this code #i m trying to get the addition of all digits of entered number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#how can correct this code #i m trying to get the addition of all digits of entered number

#how can correct this code #i m trying to get the addition of all digits of entered number n = int(input()) length = 0 while (n)>0: length =length+(n % 10) n=n/10 print(int(length))

6th Aug 2020, 10:35 AM
Abhishek
7 Answers
+ 4
Here are 2 versions that can do this task: # when input is string: inp = '1234' print(sum([int(i) for i in inp])) # when it should be done with integers inp = 1234 new = [] while inp > 0: inp, mod = divmod(inp,10) new.insert(0,mod) print(sum(new))
6th Aug 2020, 2:08 PM
Lothar
Lothar - avatar
+ 3
Abhishek What you want to do is not very clear. Addition of the digits or the length? Anyways, check this out, it might help. https://code.sololearn.com/cTgx4gYfpto8/?ref=app
6th Aug 2020, 10:58 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Sum of the digits or count of digits?
6th Aug 2020, 10:40 AM
Ipang
+ 1
n = input() print(len(n))
6th Aug 2020, 10:41 AM
JaScript
JaScript - avatar
+ 1
Abhishek Above code works fine 😃😃
6th Aug 2020, 11:33 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
I am trying to find out the addition of digits which is entered by user input Need to use while loop only Above code is giving almost correct output just getting addition of 1, If input 1234 Output should be 10
6th Aug 2020, 11:16 AM
Abhishek
0
Chk with 6 0r 7 digit entry
6th Aug 2020, 11:52 AM
Abhishek