In: Computer Science
File density.txtPreview the document contains a list of the density in grams per cubic centimeter of all the planets, plus Pluto. It looks like this: Mercury 5.43 Venus 5.25 Earth 5.52 Mars 3.83 Jupiter 1.33 Saturn 0.71 Uranus 1.24 Neptune 1.67 Pluto 2.05 Write a program named lastname_firstname_density.py that reads the numbers from the file into a list and then calculates and prints, properly labeled: The minimum density The maximum density The average density The median density (Hint: use split() to separate the planet name from the density.) You don’t need a list for the first three of these, but you do need a list to calculate the median. Finding the Median To find the median of a group of n numbers, sort them into order. (Hint: use Python’s sort method). If n is odd, the median is the middle entry. In a list named data this will be element data[(n ‑ 1) // 2]. Note the use of // to do integer division. If n is even, the median is the average of the numbers “surrounding” the middle. For a list named data, this is (data[(n // 2)] + data[(n // 2) ‑ 1]) / 2.0. Your program must work for a file with any number of entries. The file I am providing happens to have an odd number of entries, but if I gave you a similar file for the major moons of Saturn, it would have an even number of entries, and your program would still have to correctly calculate the statistics for that file.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
# main method def main(): # opening density.txt file. change filename as you like. make sure file exist on the # same directory file = open('density.txt', 'r') # initializing an empty list data = [] # and required variables min_density = 0 max_density = 0 sum_density = 0 avg_density = 0 median_density = 0 # looping through each line in file for line in file: # splitting line by ' ' fields = line.strip().split(' ') # ensuring that resultant list has length 2 (planet name and density) if len(fields) == 2: # parsing second value as density density = float(fields[1]) # appending to list data.append(density) # if this is first entry, setting this as both min and max if len(data) == 1: min_density = density max_density = density else: # otherwise updating min and max if needed if density > max_density: max_density = density if density < min_density: min_density = density # adding this to sum sum_density += density # closing file file.close() # finding average if count>0 if len(data) > 0: avg_density = sum_density / len(data) # sorting the list data.sort() # if number of elements in data is odd, choosing middle element as median if len(data) % 2 != 0: median_density = data[len(data) // 2] # otherwise finding average of middle two values else: median_density = (data[(len(data) // 2)] + data[(len(data) // 2) - 1]) / 2.0 # printing all stats print('The minimum density:', min_density) print('The maximum density:', max_density) print('The average density:', avg_density) print('The median density:', median_density) # calling main() main()
#output
The minimum density: 0.71
The maximum density: 5.52
The average density: 3.003333333333333
The median density: 2.05
#code screenshot