In: Computer Science
#Sphere – pi and radius
Create two python programs named program_1 and program_2 that will calculate the surface area of a sphere. The programs will use user-defined functions. Global variables are NOT allowed in either program. Program_1 will be listed first in the answer box with Program_2 written below it. Separate the programs with a line of dashes ( --------------------------------------------- ) as shown in the example below.
Program_1 will:
Program_2 will
#program_1
#import all function of program_2
from program_2 import *
#user defined function for calculating surface area of the sphere
def surface_area(rad):
#calls the getpivalue function from program_2 file
pi = getpivalue()
#calculates surface area of sphere that is 4 * pi * rad * rad
ans = 4 * pi * rad *rad
#return area of the sphere stored in ans variable
return ans
#takes radius eterd by user
rad = float(input("Enter the radius: "))
#prints Radius and surface area of the sphere
print("Radius: ", rad , "Surface Area: {:.2f}".format(surface_area(rad)))
-----------------------------------------------------------------------------------
#program_2
import math
#user define function that returns pi value
def getpivalue():
#stores the value of pi in pi_val variable
pi_val = math.pi
#return pi_val
return pi_val
I have run this code in pycharm editor. You can run in any editor of your choice.
program_1.py
program_2.py
OUTPUT: