#!/usr/bin/env python # # Author : Ahmed Obied (amaobied@ucalgary.ca) # Date : Jan. 17th 2008 # Purpose : Calculates the area of a circle or the volume # and surface area of a sphere. # # constant for pi PI = 3.141592654 def circle_area(radius): """ Purpose : calculates the area of a circle Parameters : radius - the radius of the circle Returns : the area """ return PI * radius * radius def sphere_volume(radius): """ Purpose : calculates the volume of a sphere Parameters : radius - the radius of the sphere Returns : the volume """ return 4.0 / 3.0 * PI * radius * radius * radius def sphere_surface_area(radius): """ Purpose : calculates the surface area of a sphere Parameters : radius - the radius of the sphere Returns : the surface area """ return 4 * PI * radius * radius def sphere_calc(radius): """ Purpose : calls the sphere_volumen and sphere_surface_area functions Parameters : radius - the radius of the sphere Returns : the volume and the surface area """ return (sphere_volume(radius), sphere_surface_area(radius)) if __name__ == '__main__': """ Main Entry """ while True: # print the banner print '\nWelcome to circle and sphere calculator' print '(c)ircle\'s surface area' print '(s)phere\'s surface area and volume' print '(q)uit the program' # get the choice choice = raw_input('Enter choice now: ') # based on the given choice, take the appropriate action if choice == 'C' or choice == 'c': # get the radius and calculate the area radius = float(raw_input('Enter the radius of the circle: ')) print 'The area of the cicle with radius %.2f is %.2f' % \ (radius, circle_area(radius)) elif choice == 'S' or choice == 's': # get the radius and calculate the volumen and area radius = float(raw_input('Enter the radius of the sphere: ')) volume, area = sphere_calc(radius) print 'The surface area of the cicle with radius %4.2f is %4.2f ' \ 'and the volume is %4.2f' % (radius, area, volume) # if 'Q' or 'q' is entered then we break out of the while loop to quit the program elif choice == 'Q' or choice == 'q': print 'Quiting program.' break