Engagement rates from swapped pairs

Code review · Medium · §10.5, §10.7 [Pair coding with AI] "Make it Pythonic"

You had a loop that computes the engagement rate (likes ÷ views × 100) of each post, asked an AI to "Make it Pythonic," and got this code. If the view count is 0, the engagement rate is 0.0. The code runs without an error, but the rates go far above 100. Fix just one line.

Code to fix

likes = [30, 0, 12, 9]
views = [120, 0, 96, 12]
rates = [like / view * 100 if view > 0 else 0.0 for like, view in zip(views, likes)]
print(rates)

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.