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...")
 
Line 10: Line 10:
 
== Directions ==
 
== Directions ==
 
Create a file name "functionsPractice.py".  In your file, complete the following exercises:
 
Create a file name "functionsPractice.py".  In your file, complete the following exercises:
 +
 +
# '''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:
 +
#** 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:
 +
#* 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

Revision as of 09:55, 3 May 2012

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

Resources

Directions

Create a file name "functionsPractice.py". In your file, complete the following exercises:

  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:
      • 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:
    • 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