Functions Objectives: calling an existing function writing a function based on a
ID: 3692748 • Letter: F
Question
Functions Objectives: calling an existing function writing a function based on a specification testing your own function Part 1 t is an important skill to test functions to ensure they work correctly. In part 1, you're given a function definition that you need to test. Download this starter code. (right-click on the link and then choose: Save Link As) . Read over the function definition. Next, edit the program so that main() calls the function two times and displays results to the user. Here are the test cases base base2 height area - expected result 36 40.5 4 7 Part 2: The next skill to practice is writing your own function definition and then testing the function. In the same file, write a new function definition that is passed in the 2 legs of a right triangle, a and b, and then calculates and returns the hypoteneuse. Do not use the math.hypot () function. Your task is to implement the math yourself. Come up with 2 tests cases with the expected results calculated by hand. Test your function (call it and display the results) to confirm that the actual results of your function match the expected results Write a function block comment for your function definition that follows the same style as the comment for the areaTrapezoid() function. . NOTE: Your program should only have 1 main() function definition and only 1 call to main() at the bottom of the file. Put all the calls to areaTrapezoid() and your hypotenuse function inside main() Submit your file when you're done. Make sure to add your name to the file.Explanation / Answer
import math
def main():
print ' ========Part 1========='
# Demonstrate several different ways to call the areaTrapezoid() function
area1 = areaTrapezoid(4, 5, 8)
print "For 4, 5, 8, the area is", area1
print "For 2, 7, 9, the area is", areaTrapezoid(2, 7, 9)
print 'For 2, 4, 6, the area is ' + str(areaTrapezoid(2, 4, 6))
print 'For 2.3, 4.1, 6.99, the area is %.2f' % areaTrapezoid(2.3, 4.1, 6.99)
# The function sin_cos_squared() is defined below; this code tests it.
# Some rows in the output show expected representational error.
print ' ========Part 2========='
sin_cos_squared(8)
sin_cos_squared(20)
# a trapezoid has a height and a top length and bottom length.
# The top and bottom lengths are typically referred to as base2 and base1
def areaTrapezoid(base1, base2, height):
area = height / 2.0 * (base1 + base2)
return area
# displays ("prints") a table containing (intervals + 1) rows
# showing values for 'n' ranging from 0 to 2pi and sin(n), cos(n), and
# (sin^2(n) + cos^2(n)) for each value of 'n'.
def sin_cos_squared(intervals):
first = 0.0
last = math.pi * 2
step = (last - first) / intervals
print ' n sin(n) cos(n) sin^2(n) + cos^2(n)'
for i in range(intervals + 1):
num = i * step
ans = math.sin(num) ** 2 + math.cos(num) ** 2
print '%.4f %6.3f %6.3f %.17f' %
(num, math.sin(num), math.cos(num), ans)
main() # call 'main' method -- starts the program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.