Learner objectives for this lesson
A commonly used method to provide a rough estimate of the right length of snowboard for a rider is to calculate 88 percent of their height. Write a program that will help people estimate the length of snowboard they should buy. Obtain the user's height in feet and inches (assume these values will be entered as integers) and display the length of snowboard in centimeters to the user. Note: There are 2.54 centimeters in an inch.
Define the following functions in your solution:
get_user_height()
: Returns the user's total height in inches.convert_inches_to_centimeters(inches)
: Returns inches
in centimeters.calculate_snowboard_length(height_inches)
: Returns the snowboard length estimate for a rider of height_inches
height.display_snowboard_length(height_inches, snowboard_cm)
: Displays the snowboard length for a rider of height_inches
.Each function is itself a small-scale "program"
It makes sense to test each function independently
A test-driver is a short program that tests a specific function
Example: Recall the function display_gpa()
:
# function definition
def display_gpa(gpa):
'''
Displays the final gpa to the user.
'''
print("Your GPA is: %.2f" %(gpa))
# function call
display_gpa(3.4)
Your GPA is: 3.40
In a function call such as display_gpa(3.4)
, the value 3.4 is considered the argument of the function. When the function is executed, the value 3.4 is copied into a new local variable, gpa
. We call gpa
a local variable because the variable is only accessible within display_gpa()
on this particular functional call (display_gpa(3.4)
).
Scope of a name: region of a program where a particular meaning of a name is visible or can be referenced.
Variables declared outside of a function are considered global. In general, global variables are a bad idea and we try to avoid using them.
Parameter variables and variables declared within a function are considered local. Local variables are only visible from within that function; once function is done, variables go away (space is deallocated). Consider the following example (which is an example used for scope purposes, not as an example of good program design!):
max_val = 950
def one(anarg, second):
'''
'''
one_local = 0
def fun_two(one, anarg):
'''
'''
local_var = 0
def main():
'''
'''
local_var = 0
one(0, 1) # function call #1
fun_two(5, 10) # function call #2
main() # function call #3
limit = 200
When each of the above functions are executed from function calls #1-3, what is the scope of the following identifiers?
Name | visible in one() when executedfrom function call #1 |
visible in fun_two() when executedfrom function call #2 |
visible in main() when executedfrom function call #3 |
---|---|---|---|
max_val |
y | y | y |
limit |
n | n | n |
main |
y | y | y |
local_var (local variable in main() ) |
n | n | y |
one (function) |
y | n | y |
anarg (parameter in one() ) |
y | n | n |
second |
y | n | n |
one_local |
y | n | n |
fun_two |
y | y | y |
one (parameter in fun_two() ) |
n | y | n |
anarg (parameter in fun_two() ) |
n | y | n |
local_var (local variable in fun_two() ) |
n | y | n |
You can define a function without adding a body by simply placing the reserved keyword pass
in the body. This can be useful when you want to test your program one function at a time or when you want to organize your program without actually writing the functions (or as a placeholder if someone else is writing the function). Example:
def quadratic_root_finder(a, b, c):
'''
Applies the quadratic equation to find the roots of a quadratic function
specified by the formula ax^2 + bx + c = 0
To efficiently be implemented by someone else!
'''
pass