Chapter Practice Arena
All ChaptersModules, Files, and Errors Problems
Targeted practice challenges specifically curated for this chapter. Inspect hints, verify outputs, and check Pythonic solutions.
Difficulty:
Chapter:
Showing 6 challenges · 20 total · Page 1 of 4
Write an add(a, b) function that returns the sum of its arguments. Check that add(2, 3) equals 5, add(0, 0) equals 0, and add(-2, 2) equals 0. Print All checks passed after the checks.
Starter Code
def add(a, b):
# Return the sum
pass
# Add the three assertions
# Print the success message
Expected Output:All checks passed
Given text = '-3', convert it to an integer. If the number is negative, print Enter a nonnegative number. Otherwise, print Accepted: followed by the number. Use an if statement rather than assert.
Starter Code
text = '-3' number = int(text) # Check whether number meets the requirement
Expected Output:Enter a nonnegative number.
Import math and print the square root of 49, followed by the ceiling of 3.1.
Starter Code
import math # Print the square root and ceiling
Expected Output:7.0
4
Write a function named divide(a, b) that raises ValueError with the message Divisor must not be zero. when b is zero. Otherwise, return a / b. Call divide(10, 0), catch ValueError, and print the exception message.
Starter Code
def divide(a, b):
# Validate b and return the quotient
pass
# Call the function and handle its error
Expected Output:Divisor must not be zero.
Create a file named helpers.py that defines double(number) and returns number * 2. In a separate file named main.py, import helpers and print helpers.double(9). Save both files in the same project directory and run main.py.
Starter Code
# helpers.py
def double(number):
# Return twice the number
pass
# main.py
import helpers
print(helpers.double(9))Expected Output:18
Calculate result = 5 * 5, assert that result equals 25, and print Verified. The program should print exactly Verified when the assertion succeeds.
Starter Code
result = 5 * 5 # Add an assertion here # Print the required message
Expected Output:Verified