Grade 10 Computer Science — Going through a file line by line
Follow a loop that visits every line of a file in turn, turns each line into a number where it needs to, and leaves a total or a count behind.
Name: ________________________
1.A decision inside this loop lets only some of the lines reach the counter. What does the program print?
f = open("sales.txt", "w") f.write("12\n30\n5\n22\n") f.close() big = 0 f = open("sales.txt", "r") for line in f: if int(line) > 20: big = big + 1 f.close() print(big)2.A program adds up the numbers in a file, one line at a time. Someone then adds one more line to the file, holding 5, and the program is run again without being changed. What is different?
- a) The loop makes one more trip, and the total is 5 larger
- b) The total is 5 larger, but the loop makes the same number of trips as before
- c) Nothing, because the program was not changed
- d) The loop makes one more trip, and the total is 1 larger
3.This loop keeps hold of the largest number it has seen so far. What does the program print?
f = open("temps.txt", "w") f.write("14\n39\n22\n") f.close() best = 0 f = open("temps.txt", "r") for line in f: if int(line) > best: best = int(line) f.close() print(best)4.The numbers saved in this file are added up as the loop meets them. What does the program print?
f = open("marks.txt", "w") f.write("8\n3\n6\n") f.close() total = 0 f = open("marks.txt", "r") for line in f: total = total + int(line) f.close() print(total)5.This loop prints as it goes rather than keeping anything back. What is the last number it prints?
f = open("steps.txt", "w") f.write("3\n5\n2\n") f.close() f = open("steps.txt", "r") for line in f: print(int(line) * 10) f.close()6.A program opens a file, goes through it with a line loop, and closes it again. Sort each of these lines of code by how often it runs.
Groups: Runs once, outside the loop · Runs once for every line in the file
- print(total)
- print(int(line) * 2)
- f.close()
- f = open("data.txt", "r")
- total = 0
- total = total + int(line)
7.The file this loop is given has nothing written in it at all. What does the program print?
f = open("empty.txt", "w") f.write("") f.close() total = 0 f = open("empty.txt", "r") for line in f: total = total + int(line) f.close() print(total)8.Nothing in these lines is a number, and the loop does not try to make one. What does the program print?
f = open("shades.txt", "w") f.write("red\ngreen\nblue\nblack\n") f.close() count = 0 f = open("shades.txt", "r") for line in f: count = count + 1 f.close() print(count)
Answer key — Grade 10 Computer Science — Going through a file line by line
- 1. 2
- 2. a) The loop makes one more trip, and the total is 5 larger
- 3. 39
- 4. 17
- 5. 20
- 6. f = open("data.txt", "r") → Runs once, outside the loop; total = 0 → Runs once, outside the loop; total = total + int(line) → Runs once for every line in the file; print(int(line) * 2) → Runs once for every line in the file; f.close() → Runs once, outside the loop; print(total) → Runs once, outside the loop
- 7. 0
- 8. 4