0

I have this script which can calculate the total of numbers given in a string

total = 0 
for c in '0123456789': 
   total += int(c) 
print total

How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify this line:

for c in '1.32, 5.32, 4.4, 3.78':

to find the total of these given numbers.

2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by Gribouillis
1

You can do it in a one-liner

total = sum(float(x) for x in '1.32, 5.32, 4.4, 3.78'.split(','))

Edited by Gribouillis

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.