Help with Python Dictionaries Challenge project

damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B'] conversion = {"M": 1000000, "B": 1000000000} def updated_damages(): updated_damages_list = [] for price in damages: if price.find("M") >= 0: updated_damages_list.append(price[0,-1] * conversion.get("M")) elif price.find("B") >= 0: updated_damages_list.append(price[0,-1] * conversion.get("B")) else: updated_damages_list.append(price) return updated_damages_list # test function by updating damages updated_damages()

im on the first exercise of the Dictionaries Challenge Project and i cant tell why im getting an error:
Traceback (most recent call last):
File “script.py”, line 37, in
updated_damages()
File “script.py”, line 30, in updated_damages
updated_damages_list.append(price[0,-1] * conversion.get(“M”))
TypeError: string indices must be integers

i have tried turning the price slice into an integer and float, ive trie turning the conversion get into an integer and a float as well im jsut not understanding what the problem is and im lost…please help :frowning:

I think that specific error is just because that syntax isn’t valid for slicing. Have a quick look at the tutorial pages for a little info if you can’t spot why-
https://docs.python.org/3/tutorial/introduction.html#strings

yeah I see it now… facepalm thanks for the help

1 Like
damages = ['Damages not recorded', '100M', 'Damages not recorded', '40M', '27.9M', '5M', 'Damages not recorded', '306M', '2M', '65.8M', '326M', '60.3M', '208M', '1.42B', '25.4M', 'Damages not recorded', '1.54B', '1.24B', '7.1B', '10B', '26.5B', '6.2B', '5.37B', '23.3B', '1.01B', '125B', '12B', '29.4B', '1.76B', '720M', '15.1B', '64.8B', '91.6B', '25.1B'] conversion = {"M": 1000000, "B": 1000000000} def updated_damages(): updated_damages_list = [] for price in damages: if price.find("M") >= 0: updated_damages_list.append(price[0:-1] * conversion.get("M")) elif price.find("B") >= 0: updated_damages_list.append(price[0:-1] * conversion.get("B")) else: updated_damages_list.append(price) return updated_damages_list # test function by updating damages updated_damages_result = updated_damages() print(updated_damages_result)

okay so I fixed that problem only to run face first into a new one, now its outputing anything, ive tryed printing updated_damages_list as well but both generate no output so i dont even know if its working and if not what the issue is.

I think it’ll be timing out/running out of memory or similar, there’s a feature of strings that the * operator between a string an integer repeats the string. So you could be repeating a string either 1000000 or 1000000000 times. Does that make sense?

Quick example:

print('Hello world!' * 10)

I agree with @tgrtim that
the price stuff should be converted to a number before multiplication.
so …

      updated_damages_list.append(price[0:-1] * conversion.get("M"))

should be:

      updated_damages_list.append(float(price[0:-1]) * conversion.get("M"))
1 Like