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. Notice the quoted value and the type of every field.
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 rows to planets.csv with csv.writer and writerows, opening the file with newline="". Then read it back with csv.reader and print how many data rows there are, not counting the header.
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 row is a dictionary and the program prints each name with its score.
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 should total the km column and print Total km: 17. It crashes on the header row. Fix it so the header is skipped.
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 city and best each time one of them changes, once both exist.
import csv
lines = ["city,temp", "Oslo,4", "Rome,19", "Cairo,31", "Lima,22"]
best = 0
for row in csv.DictReader(lines):
city = row["city"]
if int(row["temp"]) > best:
best = int(row["temp"])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: Read the rows with csv.DictReader and use column names, never number indexes like row[0].
This code works but reads columns by position, which breaks if the columns move. Rewrite it with csv.DictReader and column names, with no row[ number indexing. 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.
Read marks.csv with DictReader. Write grades.csv with DictWriter and the columns name, average and result, where average is the mean of the three marks rounded to 1 place and result is pass for 50 or more, otherwise fail. Then print the new file. The output should be:
name,average,result Asha,74.0,pass Dev,45.3,fail Noor,61.7,pass
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.