Hello! I am doing the thread shed exercise and the task 14 says: " 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.
Iterate through sales
and for each item, strip off the $
, set it equal to a float, and add it to total_sales
"
I have tried this code and it does not work:
total_sales = 0
for amount in sales:
amount.strip(“$”)
total_sales += float(amount)
Traceback (most recent call last):
File “script.py”, line 142, in
total_sales += float(amount)
ValueError: could not convert string to float: ‘$1.21’
However, the solution they give us, it is really similar and it works:
total_sales = 0
for amount in sales:
total_sales += float(amount.strip(“$”))
Can someone explain to me why mine does not work??
Thank you!!! 
Hello angelsalbiol205,
You are getting a ValueError
because you you can’t convert string to an float.
What’s happening is, “amount” is still a string when you are trying to add it to total_sales.
Your code: with comments:
sales = ['$12.00', '$4.00'] # arbitrary data.
total_sales = 0
for amount in sales:
amount.strip("$") # This does nothing because of the immutable characteristics of string data types.
# I added a print statement to show you the results:
print(amount) # $4.00
print(type(amount)) # <class 'str'>
'''
As you see, 'amount' is still a string with the dollar sign ($) attached.
You can't cast a non-digit string like this as a float.
This is why you are getting a ValueError "could not convert string to float."
'''
total_sales += float(amount)
The sample solution you provided works because the dollar sign was stripped before it is casted to a float.
In another example:
x = '$1'
print(float(x)) # ValueError
print(float(x.strip("$"))) # 1.0
The first solution doesn’t work because ‘x’ is still a string with the dollar sign attached.
The second solution works because the dollar sign is stripped from ‘x’ first before it is casted to a float.
If you want your code to work try this:
total_sales = 0
for amount in sales:
# Because strings are immutable assign a new string value to equal the desired result.
amount = amount.strip("$")
total_sales += float(amount)
print(total_sales)
I hope this helps!
3 Likes
This helps a lot! As a beginner, it’s difficult to understand why your code does not work sometimes
Thank you! 
1 Like
I tried the code but neither of them worked.
for amount in sales:
amount = amount.strip(“$”)
total_sales += float(amount)
print(total_sales)
Please format your code by reading this post → How do I format code in my posts? - Frequently Asked Questions / Codecademy FAQ - Codecademy Forums and read this on how to get answers for your questions → How to ask good questions (and get good answers) - Get Started - Codecademy Forums
Based on your code now, you did not initialize total_sales
and assign a value to it. Other than that, there might be identation error in your code.