BackPython Functions, Selection Statements, and Test Cases: Precalculus Study Context
Study Guide - Smart Notes
Tailored notes based on your materials, expanded with key definitions, examples, and context.
Python Functions and Selection Statements
Introduction
This study guide covers the definition and use of functions in Python, with a focus on selection statements and test case development. These concepts are foundational for computational problem-solving and are often used to model mathematical processes in Precalculus and other STEM courses.
Expressions: Relational Operators
Relational operators are used to compare values and produce Boolean results (True or False). They are essential in selection statements for decision-making in code.
==: Equal to
!=: Not equal to
<: Less than
>: Greater than
<=: Less than or equal to
>=: Greater than or equal to
Example: returns True if x is greater than 5.
Statements: Selection Statements and Return Statement
Selection statements allow a program to choose different paths of execution based on conditions. The most common selection statement in Python is the if statement.
if: Executes a block of code if a condition is True.
elif: Checks another condition if previous if was False.
else: Executes if all previous conditions are False.
Return statement is used in functions to send a value back to the caller.
Example:
Python code: if x > 5: return 'Greater'
Functions: Definitions and Calling
A function is a reusable block of code that performs a specific task. Functions are defined using the def keyword in Python.
Definition: def functionName(parameters):
Calling: functionName(arguments)
Example:
Defining a function: def add(a, b): return a + b
Calling the function: add(2, 3) returns 5
Testing: Developing Test Cases
Test cases are specific inputs and expected outputs used to verify that a function works correctly. Good test cases cover normal, boundary, and edge cases.
Test case example for a function calculateDamage(10, 4): Expected output is 4.
Function Implementation Examples
calculateDamage Function
This function models a simple game mechanic where two integers represent an attack roll and potential damage. The function returns damage based on the roll.
Definition: def calculateDamage(attack, damage):
If attack > 15: Return 2 × damage
Else if attack >= damage: Return damage
Else: Return 0
Test Cases:
calculateDamage(10, 4) returns 4
calculateDamage(8, 4) returns 0
calculateDamage(20, 6) returns 12
checkForEven Function
This function checks if any of three integer parameters are even and returns a string indicating which are even, or 'All even!' if all are even.
Definition: def checkForEven(a, b, c):
If any parameter is even, return a string like 'X is odd' for odd values.
If all are even, return 'All even!'
Test Cases:
checkForEven(10, 12, 14) returns 'All even!'
checkForEven(10, 12, 13) returns '13 is odd'
isBetween Function
This function checks if a value falls between two other values (inclusive).
Definition: def isBetween(a, b, c):
Returns True if c is between a and b (inclusive), else False.
Test Cases:
isBetween(4, 10, 6) returns True
isBetween(4, 10, 14) returns False
goodSteak Function
This function uses the isBetween function to determine if a steak's temperature is 'Perfect' or not.
Definition: def goodSteak(temp):
If temp is between 130 and 135 (inclusive), return 'Perfect'
Else, return 'Oops...try again!'
Test Cases:
goodSteak(132) returns 'Perfect'
goodSteak(130) returns 'Perfect'
goodSteak(150) returns 'Oops...try again!'
describePerson Function
This function classifies a person based on age into categories: child, teenager, adult, senior, or 'probably dead'.
Definition: def describePerson(age):
If age ≤ 12: return 'child'
If 13 ≤ age ≤ 19: return 'teenager'
If 20 ≤ age ≤ 59: return 'adult'
If 60 ≤ age ≤ 99: return 'senior'
If age ≥ 100: return 'probably dead'
Test Cases:
describePerson(10) returns 'child'
describePerson(19) returns 'teenager'
describePerson(27) returns 'adult'
describePerson(90) returns 'senior'
describePerson(105) returns 'probably dead'
Summary Table: Function Purposes and Test Cases
Function Name | Purpose | Example Test Case |
|---|---|---|
calculateDamage | Returns damage based on attack roll and damage value | calculateDamage(10, 4) → 4 |
checkForEven | Checks if any of three numbers are even | checkForEven(10, 12, 14) → 'All even!' |
isBetween | Checks if a value is between two others (inclusive) | isBetween(4, 10, 6) → True |
goodSteak | Determines if steak temperature is 'Perfect' | goodSteak(132) → 'Perfect' |
describePerson | Classifies age into categories | describePerson(27) → 'adult' |
Submission Instructions
Save your main Python file as main.py.
Submit to the designated platform before the due date.
Final submission is due Monday, Sep 29 at 8:00PM.
Additional info: While the assignment is focused on Python programming, the logical structure and use of selection statements are directly applicable to mathematical modeling and problem-solving in Precalculus.