[Tutor] reading strings and calculating totals

Alan Gauld alan.gauld at btinternet.com
Mon Sep 1 00:39:35 CEST 2014


On 31/08/14 19:32, Alex Kleider wrote:

>> total = 0
>> with open('/Users/richarddillon/Desktop/numbers.txt', 'r') as infile:
>>     for line in infile:
>>         total += float(line)
>> print(total)
>
> ..but isn't there a problem if the file contains empty lines?

Yes you are right. And my second reply did point out that
the OP would need to add some lines to catch any such
non-conformities in his data.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list