Difference between revisions of "Python Functions Assignment"
From WLCS
(Created page with "== Objectives == * You will learn to define functions * You will learn to call functions with input parameters * You will learn to test the data returned by functions == Resourc...") |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Objectives == | == Objectives == | ||
− | * You will | + | * You will define functions |
− | * You will | + | * You will call functions with input parameters |
− | * You will | + | * You will test the data returned by functions |
== Resources == | == Resources == | ||
+ | * [[Media:PythonFunctions.pptx]] | ||
* [http://www.openbookproject.net/thinkcs/python/english2e/ch03.html HTTLACS: Ch 3 - Functions] | * [http://www.openbookproject.net/thinkcs/python/english2e/ch03.html HTTLACS: Ch 3 - Functions] | ||
* [http://www.openbookproject.net/thinkcs/python/english2e/ch05.html HTTLACS: Ch 5 - More Functions] | * [http://www.openbookproject.net/thinkcs/python/english2e/ch05.html HTTLACS: Ch 5 - More Functions] | ||
== Directions == | == Directions == | ||
− | Create a file | + | # Open Wing101 |
+ | # Create a file named "functionsPractice.py". Define the following functions: | ||
+ | |||
+ | '''abs(x)''' | ||
+ | * returns the absolute value of x | ||
+ | * Examples: | ||
+ | ** abs(-5) -> 5 | ||
+ | ** abs(4) -> 4 | ||
+ | |||
+ | '''compare(a, b)''' | ||
+ | * returns 1 if a > b, 0 if a == b, and -1 if a < b | ||
+ | * Examples: | ||
+ | ** compare(5, 4) -> 1 | ||
+ | ** compare(7, 7) -> 0 | ||
+ | ** compare(2, 3) -> -1 | ||
+ | |||
+ | '''hypotenuse(a, b)''' | ||
+ | * returns the length of the hypotenuse of a right triangle given the lengths of legs a and b | ||
+ | * Examples: | ||
+ | ** hypotenuse(3, 4) -> 5.0 | ||
+ | ** hypotenuse(12, 5) -> 13.0 | ||
+ | ** hypotenuse(7, 24) -> 25.0 | ||
+ | |||
+ | '''intercept(x1, y1, x2, y2)''' | ||
+ | * returns the y-intercept of the line defined by points (x1, y1) and (x2, y2) | ||
+ | ** intercept(1, 6, 3, 12) -> 3.0 | ||
+ | ** intercept(6, 1, 1, 6) -> 7.0 | ||
+ | |||
+ | '''f2c(t)''' | ||
+ | * converts the temperature parameter t from Fahrenheit to Celsius and returns it | ||
+ | * If you don't remember the formula to convert, then look for it online! | ||
+ | * Examples: | ||
+ | ** f2c(212) -> 100.0 | ||
+ | ** f2c(32) -> 0.0 | ||
+ | |||
+ | '''c2f(t)''' | ||
+ | * converts the temperature parameter t from Celsius to Fahrenheit and returns it | ||
+ | * If you don't remember the formula to convert, then look for it online! | ||
+ | * Examples: | ||
+ | ** c2f(100) -> 212.0 | ||
+ | ** c2f(0) -> 32.0 | ||
+ | |||
+ | == Testing == | ||
+ | <syntaxhighlight lang="Python"> | ||
+ | # Copy and paste the following code below your function definitions | ||
+ | # You will need to add the function calls yourself | ||
+ | |||
+ | print("abs() test") | ||
+ | print(abs(3)) #3 | ||
+ | print(abs(-3)) #3 | ||
+ | |||
+ | print("compare() test") | ||
+ | # | ||
+ | # Write the compare() function calls here similar to abs() above | ||
+ | # | ||
+ | |||
+ | print("hypotenuse() test") | ||
+ | # | ||
+ | # Write the hypotenuse() function calls here similar to abs() above | ||
+ | # | ||
+ | |||
+ | print("intercept() test") | ||
+ | # | ||
+ | # Write the intercept() function calls here similar to abs() above | ||
+ | # | ||
+ | |||
+ | print("f2c() test") | ||
+ | # | ||
+ | # Write the f2c() function calls here similar to abs() above | ||
+ | # | ||
+ | |||
+ | print("c2f() test") | ||
+ | # | ||
+ | # Write the c2f() function calls here similar to abs() above | ||
+ | # | ||
+ | </syntaxhighlight> |
Latest revision as of 15:21, 24 September 2016
Contents
Objectives
- You will define functions
- You will call functions with input parameters
- You will test the data returned by functions
Resources
Directions
- Open Wing101
- Create a file named "functionsPractice.py". Define the following functions:
abs(x)
- returns the absolute value of x
- Examples:
- abs(-5) -> 5
- abs(4) -> 4
compare(a, b)
- returns 1 if a > b, 0 if a == b, and -1 if a < b
- Examples:
- compare(5, 4) -> 1
- compare(7, 7) -> 0
- compare(2, 3) -> -1
hypotenuse(a, b)
- returns the length of the hypotenuse of a right triangle given the lengths of legs a and b
- Examples:
- hypotenuse(3, 4) -> 5.0
- hypotenuse(12, 5) -> 13.0
- hypotenuse(7, 24) -> 25.0
intercept(x1, y1, x2, y2)
- returns the y-intercept of the line defined by points (x1, y1) and (x2, y2)
- intercept(1, 6, 3, 12) -> 3.0
- intercept(6, 1, 1, 6) -> 7.0
f2c(t)
- converts the temperature parameter t from Fahrenheit to Celsius and returns it
- If you don't remember the formula to convert, then look for it online!
- Examples:
- f2c(212) -> 100.0
- f2c(32) -> 0.0
c2f(t)
- converts the temperature parameter t from Celsius to Fahrenheit and returns it
- If you don't remember the formula to convert, then look for it online!
- Examples:
- c2f(100) -> 212.0
- c2f(0) -> 32.0
Testing
# Copy and paste the following code below your function definitions
# You will need to add the function calls yourself
print("abs() test")
print(abs(3)) #3
print(abs(-3)) #3
print("compare() test")
#
# Write the compare() function calls here similar to abs() above
#
print("hypotenuse() test")
#
# Write the hypotenuse() function calls here similar to abs() above
#
print("intercept() test")
#
# Write the intercept() function calls here similar to abs() above
#
print("f2c() test")
#
# Write the f2c() function calls here similar to abs() above
#
print("c2f() test")
#
# Write the c2f() function calls here similar to abs() above
#