WeCodeAcademy

Step 2: Clean the data

Write a clean function that turns raw rows into trustworthy ones: strip spaces, convert the numbers, compute each sale's revenue (units * price), and set aside rows that cannot be used, telling the user why. Never silently drop data: report what was rejected.

Examplepython
Open in editor »

Two rows were rejected (one bad number, one missing region) and eleven were kept. The try/except ValueError catches int('n/a') instead of crashing, and round(..., 2) keeps money to two decimals.

Your turn: write the code

Write to_int(text) that returns the integer in text, or None when it is not a whole number. Print to_int("12"), to_int("n/a") and to_int(" 7 ").

Quick check

What is better than silently dropping a bad row?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.