Library › Coding AI › College students
Convert two loops to comprehensions on Python 3.7
Practice: Setting limits and rules. About 5 minutes.
About this exercise
Convert the loops in active_emails and price_map to comprehensions. The lab server runs Python 3.7, so no walrus operator (:=). active_emails must keep order and duplicates (the attendance report counts them). price_map still skips blank prices, and the club treasurer now wants each price rounded to 2 decimals. Keep both docstrings word for word, make each body a single return statement, and leave load_rows alone. Return only the two functions in exactly 1 code block of at most 10 lines, under 20 words of text.
Common mistakes when you ask AI for this
- Using the walrus operator. To avoid stripping twice, the AI reaches for :=, which crashes on Python 3.7. Try: Add: "Python 3.7: no walrus operator."
- Deduping by accident. A set comprehension looks tidy but drops the duplicates the attendance report needs. Try: Add: "Keep order and duplicates: a list, not a set."
- Missing the rounding. The AI only converts what's there; it can't know the treasurer wants 2 decimals. Try: Add: "round(float(row['price']), 2); still skip blank prices."
- Rewriting the module. The AI reprints the file, drops docstrings or refactors load_rows too. Try: Add: "Only these two functions, docstrings unchanged, one return each."
What a strong prompt includes
- Names the Python version
- Keeps duplicates
- Adds the rounding
- Protects the docstrings
- Asks for only two functions
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)