Lab, Files and Data Formats
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints. Remember how JSON writes booleans, None and tuples.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Save profile to profile.json with json.dump inside a with block, then load it back with json.load into loaded and print loaded["name"] and the number of hobbies.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so the JSON is printed with two-space indentation and keys in alphabetical order.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program writes a scores file and should print {'Ana': 9, 'Ben': 7}. It raises a TypeError when reading. Fix it.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows name and total each time one of them changes, once both exist.
import json
text = '[{"name": "tea", "qty": 2}, {"name": "jam", "qty": 1}, {"name": "oats", "qty": 3}]'
total = 0
for item in json.loads(text):
name = item["name"]
total = total + item["qty"]Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Create text with json.dumps from a dictionary, not with an f-string.
This code builds JSON text by hand with an f-string, which breaks as soon as a value contains a quote. Rewrite it so text comes from json.dumps. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write save(loans, path), which writes a list of loan dictionaries to a JSON file with indent=2, and overdue(path, today), which loads the file and returns the sorted titles whose due day number is less than today. Save the starter loans, then print the overdue titles for day 10 and the number of loans in the file. The output should be:
['Emma', 'Ulysses'] 3
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.