Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to flush the output buffer immediately.
Python
print('Hello, world!', end='') import sys sys.stdout.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using write() instead of flush()
Trying to use close() which stops output
Using read() which is for input
✗ Incorrect
The flush() method forces the output buffer to be written immediately.
2fill in blank
mediumComplete the code to open a file with buffering disabled.
Python
with open('log.txt', 'w', buffering=[1]) as f: f.write('Log start\n')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1 which means default buffering
Using None which is invalid here
Using 1 which means line buffering
✗ Incorrect
Setting buffering=0 disables buffering for the file.
3fill in blank
hardFix the error in flushing the file output.
Python
f = open('data.txt', 'w') f.write('Data') f.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like flushes or flushed
Trying to call flush as an attribute
✗ Incorrect
The correct method to flush a file's buffer is flush().
4fill in blank
hardFill both blanks to create a dictionary of word lengths for words longer than 3 letters.
Python
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of length
Using less than instead of greater than in condition
✗ Incorrect
The dictionary comprehension uses len(word) for values and filters words with length greater than 3.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 0.
Python
data = {'a': 1, 'b': 0, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys instead of uppercase
Using less than or equal instead of greater than
Using keys as values instead of values
✗ Incorrect
The dictionary comprehension uses uppercase keys, values as is, and filters values greater than zero.