I'm a beginner and currently doing the "Programming for everybody" course of the University of Michigan.
I wrote this code myself for task 7.2 and to my understanding the code is basically done. But for some reason at line 7 of the output (you'll see when you start the program), the program starts adding 11 additional digits at the end of the sum, as if I just substracted "0.00000000000000001" from the sum.
I just can't wrap my head around why this happens, does anybody of you see the problem?
The text-file used for this can be found here: https://www.py4e.com/code3/mbox-short.txt
Thanks a lot.
file = ("mbox-short.txt")
text = open(file)
lcount = 0
for line in text:
line = line.rstrip()
if not line.startswith("X-DSPAM-Confidence"):
continue
lcount = lcount + 1
print()
print("Line: ", lcount)
#print(line)
a = line.find(":")
numbers = line[a+1:]
numbers = numbers.rstrip()
numbers = float(numbers)
print("New Number: ", numbers)
if lcount > 1:
print("Old sum: ", numbers_sum, "+", numbers)
numbers_sum = numbers_sum + numbers
print("New sum: ", numbers_sum)
else:
numbers_sum = numbers
numbers_avg = numbers_sum/lcount
print(numbers_avg)