I am currently working through the Thread Shed exercises.
Task 14 asks me to
Now, consider the list
sales. It is a list of strings that we want to sum. In order for us to sum these values, we will have to remove the
$, and set them equal to floats.
salesand for each item, strip off the
$, set it equal to a float, and add it to
and Task 15 asks me to print it.
My initial ‘sales’ list looks like this:
My code is
total_sales = 0 for sale in sales: total_sales += float(sale.strip("$")) print(total_sales)
This prints 1498.7400000000005.
I have two questions:
- Why is this happening? All of the numbers in the sales list stop at two decimal places, so a digit in the thirteenth ought to be impossible.
- How can it be fixed? I’ve searched through the previous questions on this exercise, and the closest answer I found was a link to Format Specification Mini-Language. That tells me how to ensure that my result is rounded to the second decimal place, but not how to actually fix the error.
Thank you very much.