Thread Shed Project

Step 14 Question:

Instruction: 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 saw the solution but am trying to understand why my code isn’t working if performing each step individually. It provides an error message in the first iteration, it’s not stripping the “$”. This is the short version of the list. Thank you.

sales = [’$1.21’, ‘$7.29’, ‘$12.52’, ‘$5.13’, ‘$20.39’]
new_sales = 0

for sale in sales:
new_sales += sale



You can either check documentation or practice in a terminal to figure out how certain methods work.

In this case, let’s observe in a terminal how strip might behave

>>> t = "Test"
>>> t #check value of t
'Test' #as expected
>>> t.strip("t")
'Tes' #as expected
>>> t #check value of t again
'Test' #aha!

So the strip method only returns a modified string, but doesn’t mutate the string itself. It’s useful to remember that in python, strings are immutable (they cannot be changed). What can happen is the variable to which a string is assigned can be re-assigned with a different value.

For example, let’s try something slightly different:

>>> t = "Test"
>>> t = t.strip("t")
>>> t

Hope this helps!

1 Like

Great - totally makes sense. Thank you for your quick reply. Everything is starting to get more complicated… :slight_smile:

1 Like