+ 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)
5 Réponses
+ 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.
+ 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))
0
Grateful for your help again!
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)
0
@Paul thank you. You are totally correct. Sorry for asking the stupid questions>_<