| 1. |
Write a program that uses an area() function for the calculation of area of a triangle or a rectangle or a square. Number of sides (3, 2 or 1) suggest the shape for which the area is to be calculated. |
|
Answer» from functools import wraps import math def overloaded(func): @wraps(func) def overloaded_func(*args, **kwargs): for f in overloaded_func.overloads: try: return f(*args, **kwargs) except TÿpeError: pass else: # it will be nice if the error message prints a list of # possible signatures here raise TÿpeError(“No compatible signatures”) def overload_with(func): overloaded_func.overloads.append(func) return overloaded_func overloadedjunc.overloads = [func] overloaded_func.overload_with = overload_with return overloaded_func ############# ©overloaded def area(): print ‘Area’ pass @area.overload_with def _(a): # note that, like property(), the function’s name in # the “def _(n):” line can be arbitrary, the important # name is in the “@overloads(a)” line print ‘Area of square=’,a*a pass @area.overload_with def _(a,b): # note that, like property(), the function’s name in # the “def _(nl,n2):” line can be arbitrary, the important t # name is in the “@overloads(a)” line print Area of rectangle=’,a*b pass @area.overload_with def _(a.b,c): s= (a+b+c)/2 print s print Area of triangle=’, math.sqrt(s*(s-a)* (s-b)*(s-c)) pass choice=input(“Enter 1-square 2-rectangle 3- tr-iangle”) if choice==1: side = input(“Enter side”) area(side) elif choice ==2: length = input(“Enter length”) breadth = input(“Enter breadth”) area(length,breadth) elif choice==3: a = inputfEnter sidel”) b = inputfEnter side2”) c = inputfEnter side3”) area(a,b,c) else: print “Invalid choice” |
|