Answered step by step
Verified Expert Solution
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.
Implement circarea
Now that you have fixed the header for circarea, 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 circareakw: # 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 rr 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:
circarearadius returns approx
circareadiameter returns approx
circarearadiusfoo returns approx
circarea crashes with AssertionError
circarearadiusdiameter crashes with AssertionError
Parameter kwd: the function keyword arguments
Precondition: the arguments are all numbers int or float
assert radius in kwdiameter in kw 'Either radius or diameter must be specified, but not both'
import math
if 'radius' in kw:
return math.pi kwradius
else:
return math.pi kwdiameter
def testcircarea:
Test procedure for function circarea
printTesting circarea
result func.circarearadius
introcs.assertfloatsequalresult
result func.circarearadius
introcs.assertfloatsequalresult
result func.circareadiameter
introcs.assertfloatsequalresult
# Test for crashes
try:
func.circarea
introcs.asserttrueFalse # We should never reach this line!
except:
pass
try:
func.circarearadiusdiameter
introcs.asserttrueFalse # 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started