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. Watch which mode each block uses.
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.
Write each item of items to shopping.txt on its own line, using with and encoding="utf-8". Then read the file back and print its contents with print(f.read(), end="").
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 each line is printed with its number and without the trailing newline, as 1: red.
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 saves a visit count and reads it back, and should print Visits: 7. It raises a TypeError. 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 amount and total each time one of them changes, once both exist.
with open("spend.txt", "w", encoding="utf-8") as f:
f.write("12\n5\n8\n")
total = 0
with open("spend.txt", encoding="utf-8") as f:
for line in f:
amount = int(line)
total = total + amountSign 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: Open the file only in with blocks and never call close().
This code works, but if an error happened before close() the file would stay open. Rewrite both parts to use with blocks and no close() calls. 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 sign(name), which appends the name as a new line to guests.txt, and guests(), which returns the names in the file as a list without newlines. Sign Ana, Ben and Ana, then print the list and the number of different guests. The output should be:
['Ana', 'Ben', 'Ana'] 2 different guests
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.