+ 1

Python - Sum of all digits error

Hi guys I'm really need help now cuz at my code, calculating only functions at under 100. I really can't find why is this not working over 101 PLz help me n = int(input()) r = n length = 0 while n>0: n //= 10 length += 1 L = int(length) - 1 Z=0 while r != 0: if not L == 0: Z += r//(10**L) r -= (10**L)*Z L -= 1 elif L == 0: Z = Z + r r = 0 break print(Z)

10th Nov 2020, 9:01 AM
Don Oh
Don Oh - avatar
3 Respostas
+ 3
Change this part: Z += r//(10**L) r -= (10**L)*(r//(10**L))
10th Nov 2020, 9:25 AM
Julia Shabanova
Julia Shabanova - avatar
+ 4
If you are trying to sum the digits in say, 257 which will be 14. You could do it this way. num = 567 add = 0 for i in str(num): add += int(i) print (add) # 18 Check out this code for other ways. https://code.sololearn.com/cTgx4gYfpto8/?ref=app Happy Coding 😀
10th Nov 2020, 9:42 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
Thank you so much Julia , Tomiwa :) Yall legend!! really appreciate it
10th Nov 2020, 10:47 AM
Don Oh
Don Oh - avatar