Transform to proportion and percentage in one line

Screen Link:
https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/10/looping-over-dictionaries

My Code:

content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197

for iteration_variable in content_ratings:
    content_ratings[iteration_variable] /= total_number_of_apps * 100

What I expected to happen:

Transformation of the proportions in the Dictionary to percentage in one line.
I did the calcuation with two statements seperated and it works well. But i think it should be possible to do the calculation with one line of code? Why it’s not running? I expected that the percentage will be stored in the Dictonary directly this way. Thanks :slight_smile:

What actually happened:

Replace this line with the output/error
```Your code doesn't seem to have the correct side-effects. Please re-check the instructions and your code.

<!--Enter other details below: -->

Your code is running. But the order of operations results in the incorrect outcome.

If a = 10 and b=100 then,

a /= b*100

is the equivalent of -

a = a/(b * 100)

in Python. Python evaluates the right side b * 100 first and then evaluates the /= part.

So, instead of the percentage being 10, you end up getting 0.01.

You can either -

  • Separate out the steps, or
  • You can use the equivalent of a = (a*100)/b, if you want it in a single line.
1 Like