[Tutor] reading strings and calculating totals

Richard Dillon dillonrw at comcast.net
Sat Aug 30 18:14:39 CEST 2014


I apologize in advance - This is my third week using Python (3.4.1 on a Mac)

I need to read a text file, convert the values into numbers and calculate a total.
The total I get doesn't match the values entered in the file.

def main():
    total = 0
    infile = open('/Users/richarddillon/Desktop/numbers.txt', 'r') 
    # read first record
    line = infile.readline()
    a = float(line)
    # read rest of records
    while line != '':
        total = total + a
        line = infile.readline()
    infile.close()
    print(total)

main()


More information about the Tutor mailing list