WEB PRACTICE CHAPTER 08

Files and Real-World Data

Ten additional coding tasks · W071–W080

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 8 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 8 starters ↓Chapter 8 worked examples ↓
WEB PRACTICE W071

Total library loans

Calculate total loans recorded for three sample books.

Reveal a hint

Convert the loan text to int before adding.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'library.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
total = sum(int(row['loans']) for row in rows)
print('Total loans:', total)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W072

Wettest day

Find the day with the highest rainfall.

Reveal a hint

max() accepts a key function.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'rainfall.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
wettest = max(rows, key=lambda row: float(row['mm']))
print('Wettest:', wettest['day'])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W073

Passing students

List students with a score of 60 or above.

Reveal a hint

Filter rows after converting score to int.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'scores.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
passing = [row['student'] for row in rows if int(row['score']) >= 60]
print('Passing:', ', '.join(passing))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W074

Shop basket total

Total all prices in a fictional stationery list.

Reveal a hint

float() converts each price field.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'shops.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
total = sum(float(row['price']) for row in rows)
print(f'Total: £{total:.2f}')

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W075

Average steps

Calculate the average steps from three days.

Reveal a hint

Divide the sum by the number of rows.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'steps.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
values = [int(row['steps']) for row in rows]
print('Average steps:', round(sum(values)/len(values), 1))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W076

Animal population

Calculate total observed animals.

Reveal a hint

Sum the count column.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'animals.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
print('Animals:', sum(int(row['count']) for row in rows))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W077

Highest-energy room

Find the room with the most recorded electricity use.

Reveal a hint

Use max with a numeric key.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'energy.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
room = max(rows, key=lambda row: float(row['kwh']))
print('Highest energy:', room['room'])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W078

Fastest route

Identify the shortest journey among three routes.

Reveal a hint

Use min() to find the smallest minutes field.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'travel.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
route = min(rows, key=lambda row: int(row['minutes']))
print('Fastest route:', route['route'])

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W079

Computing book count

Count how many sample books are classified as Computing.

Reveal a hint

Compare row["genre"] to Computing.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'books.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
count = sum(1 for row in rows if row['genre'] == 'Computing')
print('Computing books:', count)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W080

Create a CSV report

Read sample scores and create a derived pass/fail report.

Reveal a hint

csv.DictWriter can write header and row data.

Reveal example code (after trying it yourself)
from pathlib import Path
import csv

file_path = Path(__file__).resolve().parent.parent / 'datasets' / 'scores.csv'
with file_path.open(newline='', encoding='utf-8') as handle:
    rows = list(csv.DictReader(handle))
from io import StringIO
output = StringIO()
writer = csv.DictWriter(output, fieldnames=['student','result'])
writer.writeheader()
for row in rows:
    writer.writerow({'student':row['student'], 'result':'Pass' if int(row['score'])>=60 else 'Retry'})
print(output.getvalue().strip())

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.