Difference between revisions of "Python Functions Assignment"

From WLCS
 
(6 intermediate revisions by the same user not shown)
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 ==
 +
* [[Media:PythonFunctions.pptx]]
 
* [http://www.openbookproject.net/thinkcs/python/english2e/ch03.html HTTLACS: Ch 3 - Functions]
 
* [http://www.openbookproject.net/thinkcs/python/english2e/ch03.html HTTLACS: Ch 3 - Functions]
 
* [http://www.openbookproject.net/thinkcs/python/english2e/ch05.html HTTLACS: Ch 5 - More Functions]
 
* [http://www.openbookproject.net/thinkcs/python/english2e/ch05.html HTTLACS: Ch 5 - More Functions]
  
 
== Directions ==
 
== Directions ==
Create a file name "functionsPractice.py". In your file, complete the following exercises:
+
# Open Wing101
 +
# Create a file named "functionsPractice.py". Define the following functions:
  
# '''compare(a, b)'''
+
'''abs(x)'''
#* Define a compare(a, b) function that returns 1 if a > b, 0 if a == b, and -1 if a < b
+
* returns the absolute value of x
#* Test your compare function with 3 different function calls:
+
* Examples:
#** compare(5, 4)
+
** abs(-5) -> 5
#** compare(7, 7)
+
** abs(4) -> 4
#** compare(2, 3)
+
 
# '''hypotenuse(a, b)'''
+
'''compare(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
+
* returns 1 if a > b, 0 if a == b, and -1 if a < b
#* Test your hypotenuse function with the following function calls:
+
* Examples:
#* hypotenuse(3, 4)
+
** compare(5, 4) -> 1
#* hypotenuse(12, 5)
+
** compare(7, 7) -> 0
#* hypotenuse(7, 24)
+
** compare(2, 3) -> -1
# '''f2c(t)'''
+
 
#* Define a function named f2c(t) that converts the temperature parameter t from Fahrenheit to Celsius and returns it.
+
'''hypotenuse(a, b)'''
#* If you don't remember the formula to convert, then look for it online!
+
* returns the length of the hypotenuse of a right triangle given the lengths of legs a and b
#* Create your own function calls that test the function
+
* Examples:
# '''c2f(t)'''
+
** hypotenuse(3, 4) -> 5.0
#* Define a function named c2f(t) that converts the temperature parameter t from Celsius to Fahrenheit and returns it.
+
** hypotenuse(12, 5) -> 13.0
#* If you don't remember the formula to convert, then look for it online!
+
** hypotenuse(7, 24) -> 25.0
#* Create your own function calls that test the function
+
 
 +
'''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 ==
 +
<syntaxhighlight lang="Python">
 +
# 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
 +
#
 +
</syntaxhighlight>

Latest revision as of 15:21, 24 September 2016

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
#