Python Functions Assignment

From WLCS
Revision as of 00:25, 13 November 2015 by Admin (talk | contribs)

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. 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