Python Functions Assignment

From WLCS
Jump to: navigation, search

Objectives

  • You will define functions
  • You will call functions with input parameters
  • You will test the data returned by functions

Resources

Directions

  1. Open Wing101
  2. 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
#