I'm writing a program where I'm doing simple calculations from numbers stored in a file. However, it keeps on returning a ValueError. Is there something I should change in the code or how the text file is written?
The file is:
def main():
number = 0
total = 0.0
highest = 0
lowest = 0
try:
in_file = open("donations.txt", "r")
for line in in_file:
donation = float(line)
if donation > highest:
highest = donation
if donation < lowest:
lowest = donation
number += 1
total += donation
average = total / number
in_file.close()
print "The highest amount is $%.2f" %highest
print "The lowest amount is $%.2f" %lowest
print "The total donation is $%.2f" %total
print "The average is $%.2f" %average
except IOError:
print "No such file"
except ValueError:
print "Non-numeric data found in the file."
except:
print "An error occurred."
main()
and the text file it is reading of off is
John Brown
12.54
Agatha Christie
25.61
Rose White
15.90
John Thomas
4.51
Paul Martin
20.23