Question

In: Computer Science

Write in Python and as 2 seperate programs Write a program that allows the user to...

Write in Python and as 2 seperate programs

Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September 3.7 inches October 5.1 inches November 7.2 inches December 8.3 inches

Write comments through both programs (file creation program, and display program) telling what the program is doing

Solutions

Expert Solution

Program 1

rainfall = [] #empty list to store the rainfall

#months list
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

#take rainfall data as input
for i in range(12):
    data = float(input("Enter the rainfall in month {0}".format(i+1))) #take data for each month
    rainfall.append(data) #append data in list
    
max_rainfall = max(rainfall) #calculate maximum rainfall
min_rainfall = min(rainfall) #calculate minimum rainfall

print("Total Rainfall = {0} inches".format(sum(rainfall))) #total rainfall - sum of all values in list
print("Average Rainfall = {0} inches".format(sum(rainfall)/12)) #average rainfall

#maximum rainfall with month, find the index with has the maximum value, and from months list display element at that index
print("Maximum Rainfall = {0} inches in {1} month".format(max_rainfall, months[rainfall.index(max_rainfall)]))

#minimum rainfall with month, find the index with has the minimum value, and from months list display element at that index
print("Lowest Rainfall = {0} inches in {1} month".format(min_rainfall, months[rainfall.index(min_rainfall)]))

Program 2

f1 = open('rainfall_data.txt', 'r') #opening data file in read mode
f2 = open('analysis.txt', 'w') #opening new file in write mode to write the results
rainfall = [] #empty list to store rainfall data
months = [] #list to store months
content = f1.readline() # read the content of the file
content = content.split() #split the file

for i in range(0, len(content)-2, 3):
    months.append(content[i]) #store months into months list
    rainfall.append(content[i+1]) #store values into rainfall list

rainfall = [float(ele) for ele in rainfall] #convert the values to float

total_rainfall = sum(rainfall) #calculate total rainfall
average_rainfall = total_rainfall/12 #calculate average rainfall
max_rainfall = max(rainfall) #calculate maximum rainfall
min_rainfall = min(rainfall) #calculate minimum rainfall

f2.write("Total Rainfall = " + str(total_rainfall) + " inches\n") #write total rainfall to file
f2.write("Average Rainfall = " + str(average_rainfall) + " inches\n") #write average rainfall to file
f2.write("Maximum Rainfall = " + str(max_rainfall) + " in " + str(months[rainfall.index(max_rainfall)]) + " month\n") #max rainfall
f2.write("Minimum Rainfall = " + str(min_rainfall) + " in " + str(months[rainfall.index(min_rainfall)]) + " month\n") #min rainfall
    
f1.close() #close first file
f2.close() #close second file

read the file in which results was written

f = open('analysis.txt', 'r') #open the file in which result has been written

print(f.read()) #read the content
f.close() #close the file

Screenshot

Program 1

Program 2

Output

Program 1

Program 2

Comments has been given in the code. Also, program as well as the output screenshot has bee attached for the reference.


Related Solutions

Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
9) This is in Python Code a program that allows the user to manage its expenses...
9) This is in Python Code a program that allows the user to manage its expenses accepting the following commands: - Add <player_name> -> This command adds a new player to the system. Assume there can’t be repeated names. If a name already exists then an error is shown to the user. - Score <player_name> <score> -> This command adds the score to the player with the name player-name. Each score needs to be added individually - Report -> This...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT