In: Computer Science
Step by step in python please
Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate the circumference, area, and diameter (i.e., a function for each) When the program is done, have it report the number of circles, the average radius, area, diameter, and circumference to the screen (not to the output file)
import sys
def circumfrence(radius):
return 2*3.14*radius
def area(radius):
return 3.14*radius*radius
def diameter(radius):
return 2*radius
if __name__ == "__main__":
file_name = input("Enter a file name")
f = open(file_name,"r")
radius_list = []
for r in f:
radius_list.append(int(r))
#otput the result in file
out_file = input("Enter the output file name")
orig_stdout = sys.stdout
f = open(out_file, 'w')
sys.stdout = f
print("Total Circles:",len(radius_list))
print("Radius\tDiameter\tCircumference\tArea")
for i in range(len(radius_list)):
print(str(radius_list[i])+"\t"+str(diameter(radius_list[i]))+"\t"+str(circumfrence(radius_list[i]))+"\t"+str(area(radius_list[i])))
sys.stdout = orig_stdout
f.close()
avg_radius = 0
avg_diameter = 0
avg_area = 0
avg_circumference = 0
tot_circle = len(radius_list)
for r in radius_list:
avg_radius = avg_radius + r
avg_circumference = avg_circumference + circumfrence(r)
avg_diameter = avg_diameter + diameter(r)
avg_area = avg_area + area(r)
print("Total circle:",tot_circle)
print("Average Radius:",avg_radius)
print("Average Diameter:",avg_diameter)
print("Average Circumference",avg_circumference)