Question

In: Computer Science

Step by step in python please Write a program this will read a file (prompt for...

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)

Solutions

Expert Solution

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)


Related Solutions

Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
In Python, your program will read in a number (no need to prompt the user), and...
In Python, your program will read in a number (no need to prompt the user), and then reads in that number of lines from the terminal. Then the program should print an array of strings formatted in a nice regular box. So if the user inputs this: 5 Grim visaged war has smooth’d his wrinkled front And now, instead of mounting barded steeds To fright the souls of fearful adversaries He capers nimbly in a lady’s chamber To the lascivious...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT