I'm really a starter in programming. I want to prints the sum of a string s = '1.23,2.4,3.123'. I tried to use
total = 0.0
for s in '1.23,2.4,3.123':
total += float(s)
print total
but it doesn't work, could anyone help? THX a lot
You could try the following:
total = sum(float(i) for i in s.split(','))
It runs like this:
s.split(',') pulls out each 'number' in the string
float(i) for i in s... makes a float of each split value
sum() adds them all up
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With