+ 4

Sum of the separate numbers in a list

Namely, If the Input is: a=[42,12] So the output should be: [6,3] Pls help me revise my code as following: f=[] for i in range(len(a)): b=a[i] g=str(b) total=0 for i in g: c=int(i) total+=c f.append(total) print(f)

26th Mar 2018, 10:22 AM
Vicky Kuo
Vicky Kuo - avatar
5 ответов
+ 3
There are only two errors, it should be like this: a=[42, 12] f=[] for i in range(len(a)): b=a[i] print(b) g=str(b) total=0 for i in g: c=int(i) total+=c f.append(total) print(f) f.append must be unindented once. And print(f) too.
26th Mar 2018, 10:35 AM
Paul
Paul - avatar
+ 2
It is easier if you tell me what it is supposed to do, now I had to guess, I think this might do what you want: lst=[123,221,5] def num_total_sort(lst): f=[] for i in range(len(lst)): total=0 b=lst[i] g=str(b) for i in g: c=int(i) total+=c f.append(total) return f print(num_total_sort(lst))
26th Mar 2018, 12:33 PM
Paul
Paul - avatar
0
Grateful for your help again!
26th Mar 2018, 11:16 AM
Vicky Kuo
Vicky Kuo - avatar
0
@Paul :)) could you revise this one too? Thank you in advance...>< lst=[123,221,5] def num_total_sort(lst): f=[] total=0 for i in range(len(lst)): b=lst[i] g=str(b) for i in g: c=int(i) total+=c f.append(total) return f num_total_sort(lst)
26th Mar 2018, 12:23 PM
Vicky Kuo
Vicky Kuo - avatar
0
@Paul thank you. You are totally correct. Sorry for asking the stupid questions>_<
26th Mar 2018, 12:42 PM
Vicky Kuo
Vicky Kuo - avatar