Thread Shed step 13

There are certain parts of this project that are not making sense. For instance, in this step, I am converting numbers to float. But I don’t understand what I am doing wrong. I checked my code and its correct and I don’t know what else to do. here is my code for step 13:

total_sales = 0
for sale in sales: 
  total_sales += float(sale.strip("$"))

and this is what I get:

Traceback (most recent call last):
  File "", line 141, in <module>
    total_sales += float(sale.strip("$"))
ValueError: could not convert string to float: '\n$17.98'

It seems like sale contains strings that look like- "\n$17.98". The .strip() method only works on the ends (front/back) of the string. Since the "$" is not at the end it’s not going to be removed. You’d need to remove both of these elements before trying to convert it to a float.

It’s possible there’s a small mistake in a previous step that has led to sales containing strings with more characters than you expected. Try using a few print or similar to see if you can track down where you’re winding up with a value you don’t expect.

1 Like