Hurricane Analysis Step 2

Hi, please this is my code for step 2 but I don’t understand the bit with that is in bold and why it works?

shouldn’t damage[-1] = ‘M’ search for only the item at the end of the list that has ‘M’ attached to it?

def update_damages(damages):
updated_damages=
for damage in damages:
if damage == ‘Damages not recorded’:
updated_damages.append(damage)
elif damage [-1:]== ‘B’:
updated_damages.append(float(damage.strip(‘B’))*1000000000)
else:
updated_damages.append(float(damage.strip(‘M’))*1000000)
return updated_damages
updated_damages = update_damages(damages)
print (updated_damages)

yes, damage[-1] would only be one character (a string with a length of 1 in Python)
which is the last character of damage.

and yes, damage[-1] == 'M' would check whether the last thing in the string stored in damage is 'M',
just like damage[-1] == 'B' checks whether the last character in damage is 'B'.

damage[-1:] == 'B' accomplishes the same thing using slicing (meaning that it takes the last one … to the end of the string; which is still just the last character in this case).

So

elif damage[-1:] == 'B':
  updated_damages.append(float(damage.strip('B'))*1000000000)

checks whether damage ends with B,
and if it does,
take out the B at the beginning or end (using .strip ),
change it to a number (a float ),
and multiply by a billion,
then put that number in the updated_damages list.

I cannot seem to get my code to work:

def updated_damages(lst):
float_damages =
for damage in lst:
If damage[-1] == “B”:
float_damages.append(float(damage.strip(“B”)*1000000000))
elif damage[-1] == “M”:
float_damages.append(float(damage.strip(“M”)*1000000))
else:
float_damages.append(damage)
return float_damages

I’ve looked over the code many times. I am getting a syntax error in the line:
If damage[-1] == “B”:

Any help would be appreciated.
Craig

I think its a spelling error:
If should be if

This is when I feel like an idiot! Thank you SO much.

Found other errors as well. This one finally works:

def updated_damages(lst):
float_damages =
for damage in lst:
if damage[-1] == “B”:
float_damages.append(float(damage.strip(“B”))*1000000000)
elif damage[-1] == “M”:
float_damages.append( float(damage.strip(“M”))*1000000)
else:
float_damages.append(damage)
return float_damages