Computing Areas X88172


Statement
 

pdf   zip   main.py

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

  • If typetype is "rectangle", aa and bb are the width and height of the rectangle, respectively.

  • If typetype is "triangle", aa and bb are the base and height of the triangle, respectively.

  • If typetype is "circle", aa is the radius of the circle, and bb 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 π\pi, 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