Hello,
In order to understand exactly what I am doing rather than mindlessly inputing the instructions, I’m trying to figure out what is happening conceptually. In other words, what does the code that I’m writing actually mean?
Here are the instructions:
Count the number of times each unique content rating occurs in the data set.
- Create a dictionary named
content_ratings
where the keys are the unique content ratings and the values are all 0 (the values of 0 are temporary at this point, and they’ll be updated). - Loop through the
apps_data
list of lists. Make sure you don’t include the header row. For each iteration of the loop:
- Assign the content rating value to a variable named
c_rating
. The content rating is at index number10
in each row. - Check whether
c_rating
exists as a key incontent_ratings
. If it exists, then increment the dictionary value at that key by1
(the key is equivalent to the value stored inc_rating
).
- Outside the loop, print
content_ratings
to check whether the counting worked as expected.
Here is the answer (I unfortunately deleted the incorrect code that I originally wrote before checking the answer so I can’t provide it this time):
content_ratings = {'4+':0, '9+':0, '12+':0, '17+':0}
for row in apps_data[1:]:
c_rating = row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1
print(content_ratings)
For the part of the code that is in bold (content_ratings[c_rating] += 1), what does that mean? I’ve been pouring over that code and its corresponding instructions and it just isn’t clicking. Thanks for the help.