(PP-8)Open a new file in IDLE (“New Window” in the “File” menu) and save it as geometry.py in the directory where you keep the files you create for this course. Then copy the functions you wrote for calculating volumes and areas in the “Control Flow and Functions” exercise into this file and save it. Now open a new file and save it in the same directory. You should now be able to import your own module like this: import geometry Try and add print dir(geometry) to the file and run it. Now write a function pointyShapeVolume(x, y, squareBase) that calculates the volume of a square pyramid if squareBase is True and of a right circular cone if squareBase is False. x is the length of an edge on a square if squareBase is True and the radius of a circle when squareBase is False. y is the height of the object. First use squareBase to distinguish the cases. Use the circleArea and squareArea from the geometry module to calculate the base areas.

 #geometry.py

import math

def sphere_area(r):

    print("Area of sphere:",4*math.pi*r*r)

    return

def sphere_volume(r1):

    print("Volume of sphere:",(4/3)*math.pi*r1*r1*r1)

    return

def circle_area(r2):

    print("Area of Circle:",math.pi*r2*r2)

    return

def square_volume(h):

    print("Volume of Square:",h*h*h)

    return


#geo.py

import geometry

def pointyshapevolume(r,h,squarebase):

    if squarebase:

        squarebase=geometry.square_volume(h)

    else:

        squarebase=geometry.circle_area(r)

    return squarebase

print(dir(geometry))

print(pointyshapevolume(4,2,True))

print(pointyshapevolume(4,3,False))


    



Comments

Popular posts from this blog

python(BI)

Prac_8(AMP)

LSA10