Hey there! In this quest, we're going to make a special helper in Python called a 'function'.
Imagine you have two numbers, like 5 and 3. Our function will take these two numbers and give us back their sum.
Hey there! In this quest, we're going to make a special helper in Python called a 'function'.
Imagine you have two numbers, like 5 and 3. Our function will take these two numbers and give us back their sum.
Now it's your turn to write the code! We need to make the add
function inside calculator.py
work.
Remember, a function is like a mini-program that does a specific job. Our add
function needs to take two numbers and give back their sum.
Here's an example of how you might make a function that multiplies two numbers. You can use this idea to help you with your add
function:
def multiply(num1, num2):
# This function takes two numbers and multiplies them.
product = num1 * num2
return product
Your task is to complete the add
function so it correctly adds the two numbers it receives.
Great job on starting your add
function! Now, let's write some tests to make sure it works exactly as we expect.
Tests are like little checks that help us know if our code is doing the right thing. We're going to create a new file called test_calculator.py
in the same folder as your calculator.py
file.
Copy and paste the following code into your new test_calculator.py
file:
import pytest
from calculator import add
def test_add_positive_integers():
"""Test adding two positive integers."""
assert add(2, 3) == 5
assert add(100, 200) == 300
def test_add_negative_integers():
"""Test adding two negative integers."""
assert add(-2, -3) == -5
assert add(-100, -50) == -150
def test_add_positive_and_negative_integers():
"""Test adding a positive and a negative integer."""
assert add(5, -3) == 2
assert add(-10, 3) == -7
def test_add_with_zero():
"""Test adding with zero."""
assert add(0, 5) == 5
assert add(5, 0) == 5
assert add(0, 0) == 0
assert add(-5, 0) == -5
assert add(0, -5) == -5
def test_add_floating_point_numbers():
"""Test adding floating-point numbers."""
assert add(2.5, 3.5) == 6.0
assert add(1.0, 2.0) == 3.0
assert add(0.1, 0.2) == pytest.approx(0.3)
assert add(-1.5, 0.5) == pytest.approx(-1.0)
assert add(10.0, -2.5) == pytest.approx(7.5)
This code will help you check if your add
function is working correctly for different kinds of numbers!
Once you've saved this file, you're ready for the next step: running your tests!
Now that you've written your add
function and created your test_calculator.py
file, let's run the tests one more time to see if everything is working correctly!
Open your terminal and type this command:
pytest module-1.1/blueprint-6/quest-10
What you should see:
test_add_positive_integers
test_add_negative_integers
test_add_positive_and_negative_integers
test_add_with_zero
test_add_floating_point_numbers
If all the tests show ✅, it means your add
function is working perfectly!
Now, let's review your code with your mentor. Be ready to talk about:
add
function.test_calculator.py
file.add
function.This document provides a quick reference for defining Python functions and writing basic tests using pytest
.
A function is a block of code that performs a specific task. You define a function using the def
keyword.
def function_name(parameter1, parameter2):
"""
This is a docstring. It explains what the function does.
Args:
parameter1: Description of the first parameter.
parameter2: Description of the second parameter.
Returns:
Description of the value the function returns.
"""
# Function body starts here, indented
# Code to perform the task
result = parameter1 + parameter2
return result # Use return to send a value back
def
: Keyword to start a function definition.function_name
: Choose a descriptive name.(parameter1, parameter2)
: Input values the function accepts. Can be zero or more.:
: Marks the end of the function header."""..."""
): Explains the function's purpose, arguments (Args
), and return value (Returns
). Good practice for documentation.return
: Sends a value back from the function. If return
is not used, the function returns None
by default.Testing helps verify that your code works as expected. pytest
is a framework for writing and running tests in Python.
test_
or ending with _test.py
.test_
.A test function calls the code you want to test and uses assert
statements to check if the results are correct.
# Example test function structure
def test_something():
# Call the function you are testing
actual_result = your_function(input_value)
# Use assert to check if the actual result matches the expected result
assert actual_result == expected_result
def test_...
: Defines a test function. pytest
finds and runs functions named this way.assert condition
: Checks if condition
is True
. If it's False
, the test fails.You can include multiple assert
statements within a single test function to check different inputs or cases for the same function.
# Example test function with multiple assertions
def test_example_function():
# Test case 1: Positive input
assert example_function(5) == 25
# Test case 2: Negative input
assert example_function(-3) == 9
# Test case 3: Zero input
assert example_function(0) == 0
This approach helps ensure your function handles various situations correctly. If any assert
fails, the test function stops and is marked as failed.
Navigate to your project directory in the terminal and run the command:
pytest
pytest
will discover and run your test files and functions, reporting the results (PASSED, FAILED, etc.).