In: Computer Science
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to be processed. If ?≤0 your program must display an error message and terminate; otherwise it does the following for ? times:
#user input for number of spheres
noOfSpheres = int(input("How many spheres? "))
#if no of spheres is 0 or less it will terminate with error msg
if(noOfSpheres <= 0) :
print("Number of spheres must be 1 or more.")
#to terminate program
quit()
#to store volume of each sphere
volumeList = []
#user input of volume for each sphere
for i in range(noOfSpheres):
#it will ask for volume and store it in volume variable
volume = input("Enter Volume for shere " + str(i+1) + " in cubic centimeters : ")
#add entered volume in volumeList
volumeList.append(volume)
#to store area for each sphere
areaList = []
#constant value of PI
PI = 3.14159
#to store average surface area
avg = 0
#to maintain sphere counter
i = 0
#find surface area for each sphere and calculate average surface area
for volume in volumeList :
#to calculate radius
radius = ((3 * float(volume))/(4 * PI))**(1/3)
#to calculate area
area = 4 * PI * (radius)**2
#to calculate total sum of surface area
avg = avg + area
#sphere counter is increamented by 1
i = i + 1
#prints area of each sphere
print("area for sphere " + str(i) + " is : " + str(area))
#add area in areaList
areaList.append(area)
#calculating final average sphere area value
avg = avg / noOfSpheres
#prints avearge sphere surface area
print("Average area for all spheres : " + str(avg))
Sample output :