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 DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
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...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT