Difference between revisions of "Python Functions Assignment"

From WLCS
Line 1: Line 1:
 
== Objectives ==
 
== Objectives ==
* You will learn to define functions
+
* You will define functions
* You will learn to call functions with input parameters
+
* You will call functions with input parameters
* You will learn to test the data returned by functions
+
* You will test the data returned by functions
  
 
== Resources ==
 
== Resources ==
Line 10: Line 10:
  
 
== Directions ==
 
== Directions ==
Create a file name "functionsPractice.py". In your file, complete the following exercises:
+
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
  1. 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)
  2. 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)
  3. 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
  4. 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