qw="2,4,3.56,6,7" d=qw.split(',') print(d) c="+".join(d) print(c) w=float(d) for i in d: print(sum(w)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

qw="2,4,3.56,6,7" d=qw.split(',') print(d) c="+".join(d) print(c) w=float(d) for i in d: print(sum(w))

How to add the elements in qw, what's wrong in above code.

16th Apr 2020, 10:49 AM
somanadh
3 Answers
0
qw="2,4,3.56,6,7" d=qw.split(',') print(d) c="+".join(d) print(eval(c)) This evaluates it
16th Apr 2020, 11:17 AM
Justus
Justus - avatar
0
This is one way. Your list of strings need to be converted to float before you iterate over it. https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-converting-all-strings-in-list-to-integers/ sum=0 for i in d: sum+=float(i) print(sum)
16th Apr 2020, 11:18 AM
Avinesh
Avinesh - avatar
0
Thank you,I understand now
16th Apr 2020, 1:35 PM
somanadh