Difference between revisions of "Python Functions Assignment"
From WLCS
m (moved Python Function Exercises to Python Functions Assignment) |
|||
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 == | ||
Line 10: | Line 10: | ||
== Directions == | == Directions == | ||
− | Create a file | + | 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)''' | # '''compare(a, b)''' |
Revision as of 00:31, 13 November 2015
Objectives
- You will define functions
- You will call functions with input parameters
- You will test the data returned by functions
Resources
Directions
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)
- Define a compare(a, b) function that returns 1 if a > b, 0 if a == b, and -1 if a < b
- Test your compare function with 3 different function calls. Make sure each gives you the expected result:
- compare(5, 4)
- compare(7, 7)
- compare(2, 3)
- hypotenuse(a, b)
- Define a hypotenuse(a, b) function that returns the length of the hypotenuse of a right triangle given the lengths of legs a and b
- Test your hypotenuse function with the following function calls. Make sure each gives you the expected result:
- hypotenuse(3, 4)
- hypotenuse(12, 5)
- hypotenuse(7, 24)
- f2c(t)
- Define a function named f2c(t) that 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!
- Create your own function calls that test the function
- c2f(t)
- Define a function named c2f(t) that 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!
- Create your own function calls that test the function