Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1with open('f.txt','w') as f: f.write(42) | AOk: Write list of strings | |
| 2with open('f.txt','w') as f: pass f.write('hi') | BOutput: (file has 'AB') | |
| 3with open('f.txt','w') as f: f.write('Hello\n') | CValid: Writes with newline | |
| 4lines = ['a\n','b\n'] f.writelines(lines) | DError: File already closed | |
| 5with open('f.txt','w') as f: n = f.write('Hi') print(n) | EOutput: 2 | |
| 6with open('f.txt','w') as f: f.write('A') f.write('B') | FError: write() needs a string |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
.write(string) — Writes a string to the file.~~.writelines(list)with open('f.txt', 'w') as f:
f.write('Hello')with open('f.txt', 'a') as f:
f.write('Append this')f.write('Line 1\n')score = 95
f.write('Score: ' + str(score))lines = ['a\n', 'b\n']
f.writelines(lines)