Computing Areas X88172


Statement
 

pdf   zip   main.py

html

Write a function area(type,a,b) that receives a string type (which may be "rectangle", "triangle", or "circle") and two real numbers a and b, and computes the area of the given shape.

  • If type is "rectangle", a and b are the width and height of the rectangle, respectively.
  • If type is "triangle", a and b are the base and height of the triangle, respectively.
  • If type is "circle", a is the radius of the circle, and b is ignored.

Important: Only the function is expected. If you submission includes a main program, it has to be either commented out, or under the condition     if __name__ == "__main__":

Observation To get the value of π, add a line with ’import math’ at the top of your file, and then use math.pi when needed.

Sample session
>>> area('rectangle', 3.1, 4.0)
12.4
>>> area('triangle', 6.0, 2.0)
6.0
>>> area('rectangle', 8.2, 8.2)
67.24
>>> area('circle', 1.0, 0.0)
3.141592653589793
Information
Author
Language
English
Official solutions
Python
User solutions
Python