Sum of digits using list append


#1

I am trying to fid sum of digits by appending each digit to list and then finding out the sum of the list in some range t …

t=input("")


while t>0:
    sum=0
    q=[]
    n=input("")
    print sum(int(i) for i in n)
    t-=1

but am getting an error saying…

File "sum_digits1.py", line 7, in <module>
    print sum(int(i) for i in n)
TypeError: 'int' object is not iterable

can anyone point me as to where the error is …
appreciate the help…


#2

Why do you have two loops? you have while and a generator expression (int(i) for i in n). One loop should be enough.

don’t use input in python2, it will make the input evaluate as code (security risk). Use raw_input and convert to integer.

but you can’t loop over integers (they are not iterable, like the error message tells you), but you can loop over string and then convert each digit inside the loop to an integer


#3

in the question the input t is for the range of nos

this is the modified code

t=input("")


while t>0:
    sum=0
    q=[]
    n=raw_input("")
    for i in n:
        q.append(i)
        i=int(i)
        print sum(q)
    t-=1




but now i is not callable??

sorry im n ew to this…


#4

Again, why do you have two loops? Now for and while

we can first get the user input, then loop over the user input (which must be string), then convert the individual digits inside the loop to integers, and append them to the list

then after the loop, get the sum() of the list

which can all be done with a single loop, and a single raw_input, no further inputs needed


#5


sum=0
n=raw_input("")
for i in n:
    q=[]
    i=int(i)
    q.append(i)
  
print sum(q)




i m not getting it pls help…


#6

here:

sum=0

this will overwrite the built-in sum() function which you attempt to use here:

print sum(q)

because you made sum an integer, and integers can’t be called like function, you get an integer not callable error


#7


t=input("")

while t>0:

    q=[]
    ans=0
    n=raw_input("")



    for i in n:
        i=int(i)
        q.append(i)
        t-=1


    print sum(q)


did this

i initialized t to take number of inputs…(according to question…)

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.

Output
Calculate the sum of digits of N.

Constraints
1 ≤ T ≤ 1000
1 ≤ N ≤ 1000000
Example
Input
3
12345
31203
2123
Output
15
9

8

hope its ok

thnx a lot


#8

Why do you have two loops all of sudden? this code:

n=raw_input("")
for i in n:
    q=[]
    i=int(i)
    q.append(i)
  
print sum(q)

was so much better, if at least you define q outside the loop, this way it will have all the values, not only the last one.

by defining q inside the loop, the array becomes empty again at the beginning of each iteration. Which we do not want


#9


t=input()
while t>0:

  n=raw_input("")
  q=[]
  for i in n:
    i=int(i)
    q.append(i)
    
  print sum(q)
  t-=1

greatly appreciate yr help @stetim94

PS t is for taking no of inputs…


#10

that is an important detail which you could have mentioned earlier, it explains the presence of the second loop

nevertheless, i still wouldn’t use input() given it evaluates the result as code, which is a security risk for your program, use raw_input and convert to integer