Hurricane Analysis Step 2: String Conversion

This question is in reference to the following exercise: https://www.codecademy.com/paths/data-analyst/tracks/dacp-python-fundamentals/modules/dscp-python-dictionaries-challenge-projects/projects/hurricane-analysis

I am having a really tough time getting started with this exercise. It appears that I need to take a list and build a function that populates it with string values that are being converted to float using a dictionary.

My thoughts went something like:

  1. Build a function that iterates through the characters of the strings and multiplies the values by the appropriate dictionary value if that character matches (“M” and “B”). Then converts it to a float and adds the new value to a new list.
  2. If it is not a a numerical value, then add it to the list to capture the “Damages not recorded” strings.
  3. Print the list after it has run through all the iterations.

I am running into a couple problems and I am really hard-stuck for the first time during the lessons, I have googled, reviewed the cheat sheets and looked at some python library documentation.

My problems:

  1. How do I convert a string to a float while retaining the other strings (“Damages not recorded”)?
  2. How do I call a dictionary to modify a string (aka multiply the dictionary values to the list values) inside the function?

I am not looking for someone to write the code for me. I am more looking for people to help me get started so I can progress pass this first step and move on. Maybe my thinking is flawed or I am over complicating an easy problem but for now I have just stutter-stepped for 4 hours after attempting several of my own solutions and have gotten nowhere.

Thank you!

For point 1 it sounds like you want the behaviour of your code to change if a certain condition is met, hint hint :slightly_smiling_face:. As for point 2 I might be mistaking your intention but you can mutate a dictionary as you wish-

my_dict = {"test": "red", "test2": "blue"}
my_dict["test"] = "yellow"
my_dict["test"] += "ish"

I think one of the difficulties with this step is that you’re trying to perform several tasks some of which you may not have attempted before inside a function. If you break this down into manageable steps e.g. test a simpler code to convert “200M” to your float; then add a step that only does this for valid numbers, then do these same steps with a dictionary, then a dictionary with several values and so on you might find things easier.

Sorry if that’s a little roundabout or missed your point but feel free to add further queries as needs be.

Hey tgrtim,

Thanks for getting back to me! I am struggling to figure out how to call the dictionary values into the function to update lists. After looking back through all of the dictionary lessons I do not think this was ever practiced. Additionally, I am not even sure exactly what to search for because all of my searches yield results about updating dictionaries, which I am familiar with.

image

This is the dictionary I am provided in the lesson. Once I am done searching for the suffixes of “M” or “B” I need to use those dictionary values to modify the list. This is the step where I am completely hung up.

You can access the values in the dictionary like this:

conversion = {'M':1000000, 'B': 1000000000}
def convert_suffix(lst):
  new_damages = []
  for i in lst:
    if i.endswith("B"):
      i = i[:-1]
      i = float(i) * conversion['M']
      new_damages.append(i)
    elif i.endswith("M"):
      i = i[:-1]
      i = float(i) * conversion['B']
      new_damages.append(i)
    else:
      new_damages.append(i)
  return new_damages

Oh wow, I had never seen the .endswith call. This is incredibly helpful!

Thank you for your response!

P.S. When I go to call the function using our damages list it is giving me an Attribute error saying that “float” object has no objects “endwith”"

new_damages = []

def damage_conversion(lst):

  for i in lst:

    if i.endswith("B"):

      i = i[:-1]

      i = float(i) * conversion['B']

      new_damages.append(i)

    if i.endswith("M"):

      i = i[:-1]

      i = float(i) * conversion['M']

      new_damages.append(i)

    else:

      new_damages.append(i)

  return new_damages

damage_conversion(damages)

Try this to coerce the float to a string for the .endswith method:

conversion = {'M':1000000, 'B': 1000000000}
def convert_suffix(lst):
  new_damages = []
  for i in lst:
    if str(i).endswith("B"):
      i = i[:-1]
      i = float(i) * conversion['M']
      new_damages.append(i)
    elif str(i).endswith("M"):
      i = i[:-1]
      i = float(i) * conversion['B']
      new_damages.append(i)
    else:
      new_damages.append(i)
  return new_damages

Thank you so much for your help. That worked like a dream

To be honest, I should have looked at the code a little longer on my own as that was something I have seen before.

You are awesome and thanks for your time!

No problem, I’m happy I could help!

Thanks this worked for me, i changed a little bit the arrange of the ifs but it worked well
thanks