Assume the file containing a series of integers is named numbers.txt and exists on the computers disk. Write a program that reads all of the numbers stored in the file and calculates their total.
The program is not giving any errors back, but I'm getting the wrong total. I'm getting 5,750,884.00 and should be getting 284.00
This is what I've come up with so far:
def main():
# Accumulator.
total=0.0
try:
# Open the file
infile = open('numbers.txt', 'r')
#read the values from the file and accumulate them.
for line in infile:
amount = float(line)
total+=amount
# Close the file.
infile.close()
except exception as error:
print(err)
else:
# Print the total.
print(format(total,',.2f'))
main()