+ 2
Why it's not working properly ??
Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer. Examples 16 --> 1 + 6 = 7 942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6 132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6 493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2 My code: a = input() s = 0 while len(a) >1: for j in a: s = s+int(j) a = str(s) print(a)
5 Answers
+ 3
Shahir one line is out of place. You need to move s = 0 inside the while loop.
+ 2
If s does not get reset to 0 inside the while loop then it forever increments.
s=0
s=s+9+4+2=15
s=s+1+5=21
s=s+2+1=24
s=s+2+4=30...
+ 2
đđ
+ 1
# Shahir try this
def sum(s):
t=0
for x in list(s): t+=int(x)
if t<10: return str(t)
else: return sum(str(t))
# Testing...
print(sum("16")) #7
print(sum("942")) #6
print(sum("132189")) #6
print(sum("493193")) #2
print(sum("6")) #6
# https://code.sololearn.com/cNEsbemtUy34/?ref=app
0
But what is wrong with my code