Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start logging errors to a file named 'app.log'.
No-Code
logging.basicConfig(filename=[1], level=logging.ERROR) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong filename string.
Forgetting to put quotes around the filename.
✗ Incorrect
The filename parameter should be set to 'app.log' to log errors to that file.
2fill in blank
mediumComplete the code to log an error message 'File not found'.
No-Code
logging.[1]('File not found')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using logging.info() instead of logging.error().
Using logging.debug() which is for debugging, not errors.
✗ Incorrect
Use logging.error() to log error messages.
3fill in blank
hardFix the error in the code to catch exceptions and log them.
No-Code
try: open('data.txt') except Exception as [1]: logging.error('Failed to open file')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using reserved words like 'exception' as variable names.
Using names that are not valid identifiers.
✗ Incorrect
The variable name after 'as' is commonly 'e' to represent the exception.
4fill in blank
hardFill both blanks to log the exception message with traceback.
No-Code
except Exception as [1]: logging.error('Error occurred: %s', [2], exc_info=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the two blanks.
Not passing the exception variable to the logging call.
✗ Incorrect
The exception variable 'e' is used to log the error message and traceback.
5fill in blank
hardFill both blanks to create a dictionary comprehension that logs only errors with code greater than 400.
No-Code
error_logs = [1]: [2] for } in logs.items() if {{BLANK_2} > 400
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using curly braces to start the dictionary comprehension.
Using wrong variable names for keys or values.
✗ Incorrect
The dictionary comprehension starts with '{k:', uses 'code' as value, and 'k' as key variable.