Chapter Practice Arena
All ChaptersTesting, Quality, and Packaging Problems
Targeted practice challenges specifically curated for this chapter. Inspect hints, verify outputs, and check Pythonic solutions.
Difficulty:
Chapter:
Showing 6 challenges · 17 total · Page 1 of 3
Convert the string 'hello' to an integer. Catch ValueError and log a message with the traceback using logger.exception(). After handling the error, print Done.
Starter Code
import logging import sys # Configure logging and handle the conversion error.
Expected Output:ERROR:Integer conversion failed
A traceback containing ValueError is also logged.
Done
Assume the calculator package is installed or importable. Import add from calculator and print the result of add(6, 4).
Starter Code
# Write your solution below
Expected Output:10
For each task, name the most suitable file or location: (1) explain how to install and use the project, (2) store build-system and project metadata, (3) define arithmetic functions, and (4) write automated tests.
Starter Code
# Answer in comments or in your own notes.
Expected Output:1. README.md
2. pyproject.toml
3. src/calculator/operations.py
4. tests/test_operations.py
Write the command that installs pytest using the pip associated with the currently selected Python interpreter. Then write a command that checks which pip installation is being used.
Starter Code
# Install pytest # Check pip location
Expected Output:pip attempts to install pytest and then displays the pip version and installation location.
Write a function multiply(a, b) and a unittest.TestCase class with a test method that verifies multiply(3, 4) returns 12. Run the tests using unittest.main().
Starter Code
import unittest
def multiply(a, b):
# Implement multiplication
pass
class MultiplyTests(unittest.TestCase):
def test_multiply(self):
# Add an assertion
pass
if __name__ == '__main__':
unittest.main()Expected Output:The test runner reports one passing test, similar to:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
The function subtract(a, b) accidentally returns a + b. Write a test that expects subtract(8, 3) to return 5. Run it and explain why it fails before correcting the function.
Starter Code
import unittest
def subtract(a, b):
return a + b
class SubtractTests(unittest.TestCase):
def test_subtracts_numbers(self):
# Write an assertion that exposes the defect
passExpected Output:After correcting subtract(), the test runner reports one passing test, similar to:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK