I have this program which calculates the total for all the values entered by the user:
result = 0
for i in range(3):
n = float(input("Enter a value: "))
result = result + n
print(result)
If I entered in the values, 1.2, 1.3, 1.3, the output will be correct and print a result of 3.8. However, it seems when I enter in three floating point values which are all the same, I will get a floating point error. For example, 1.2, 1.2, 1.2 will print out 3.5999999999999996.
Why does this seem to happen? Is there a way that I can prevent it?