Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 9 . Implement circ _ area Now that you have fixed the header for circ _ area, it is time to implement the function.

19. Implement circ_area
Now that you have fixed the header for circ_area, it is time to implement the function. Again, pay close attention to the specification. If you are having trouble understanding the specification, look at the test cases in tests.py. When you have implemented the function, test your answer with the test script. You should pass all tests (for now).
def circ_area(**kw): # The parameter is MISSING. Add it back.
"""
Returns the area of the specified circle, defined by the keywords in kwd
The area of a circle is PI r*r where r is the radius
The circle may be specified by 'radius' or 'diameter', but not both. This function
should intentionally crash (with an AssertionError) if neither 'radius' nor 'diameter'
are specified, or if they both are.
Any keyword arguments other than 'radius' or 'diameter' are ignored.
Examples:
circ_area(radius=3) returns approx 28.27433
circ_area(diameter=4) returns approx 12.56637
circ_area(radius=3,foo=20) returns approx 28.27433
circ_area() crashes with AssertionError
circ_area(radius=2,diameter=4) crashes with AssertionError
Parameter kwd: the function keyword arguments
Precondition: the arguments are all numbers (int or float)
"""
assert ('radius' in kw)^('diameter' in kw), 'Either radius or diameter must be specified, but not both'
import math
if 'radius' in kw:
return math.pi * kw['radius']**2
else:
return math.pi *(kw['diameter']/2)**2
def test_circ_area():
"""
Test procedure for function circ_area().
"""
print('Testing circ_area()')
result = func.circ_area(radius=3)
introcs.assert_floats_equal(28.27433,result)
result = func.circ_area(radius=2)
introcs.assert_floats_equal(12.56637,result)
result = func.circ_area(diameter=4)
introcs.assert_floats_equal(12.56637,result)
# Test for crashes
try:
func.circ_area()
introcs.assert_true(False) # We should never reach this line!
except:
pass
try:
func.circ_area(radius=3,diameter=6)
introcs.assert_true(False) # We should never reach this line!
except:
pass
# Add a test with additional arguments

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions