A program works through the text file data.txt one number at a time and shows a number only when it is anything other than 0. Figure 6 is the program with five gaps.
Complete Figure 6 by choosing OCR Exam Reference Language statements from the box for gaps 1 to 5. Not every word is used.
Our own questions on this topic, marked straight away, so you see what to revise. Free now.
Show model answer for 04.1Hide model answer for 04.1
myFile = open("data.txt")
while NOT myFile.endOfFile()
temp = myFile.readLine()
if int(temp) != 0
print(temp)
endif
endwhile
myFile.close()Gap 1: open
Gap 2: myFile.readLine()
Gap 3: temp
Gap 4: print
Gap 5: myFile.close()How the marks are earned
- One mark for each gap, up to 5. Only one choice per gap: a gap with several options chosen earns nothing.
- BP1, gap 1: open.
- BP2, gap 2: myFile.readLine(). The form readLine is credited.
- BP3, gap 3: temp. The choice myFile.readLine() is credited here only if it was not used in gap 2.
- BP4, gap 4: print.
- BP5, gap 5: myFile.close(). The form close is credited.
- Spelling slips, capital letters and minor differences are ignored as long as it is clear which option was chosen.
Why it scores
open opens the file for reading. Each pass of the loop reads one line into temp, tests it as a number, and prints it only if it is not 0. close finishes with the file once the loop ends.
The five answers in gap order. Words such as data, txt, x and input() are the unused ones.
Revise File HandlingIdentify how this algorithm uses casting.
See how you would score on Data TypesShow model answer for 04.2Hide model answer for 04.2
int(temp) converts the text read from the file into an integer, so it can be compared with 0.
How the marks are earned
- It converts the data to an integer (to int).
- A vague answer such as changes it to a different data type earns nothing.