Python 3 Program - Write another program to read data from a file named Precipit
ID: 3717603 • Letter: P
Question
Python 3 Program - Write another program to read data from a file named Precipitation.txt. The program will ask the user two questions and then will search through the data from the file (use lists) to pull only the data that applies. 1. The program will ask the user to select a month to determine maximum precipitation and then it will print the user’s month selection, the date of maximum precipitation in that month, and the amount of precipitation for that date as read from the data in the file. 2. The program will ask the user to select a month and will then determine the total and average precipitation for the month selected by the user. The program will print the month selected by the user, the average amount of precipitation during that month (formatted to 2 decimal places) and the total amount of precipitation during that month (formatted to 2 decimal places).
Explanation / Answer
'''
CODE IN PYTHON 2.7
FORMAT OF PRECIPITATION.TXT
YYYY-MM-DD:precipitation_value
Sample data of file:
2018-10-10:13.0444
2018-10-13:33.0334
2018-01-12:344.0
2018-02-12:12.0
2018-03-19:30.0
'''
def format_date(dat):
#start of fun
return str(dat[0])+"-"+str(dat[1])+"-"+str(dat[2])
#end of fun
f = open("precipitation.txt","r")
x = f.readline()
data = []
l =0
while(x):
#start of while
spl = x.split(":")
dat = map(int,spl[0].strip().split("-"))
prec_value = float(spl[1][:-1])
data.append([dat[0],dat[1],dat[2],prec_value])
l+=1
x = f.readline()
#end of while
print data
print "1.Enter Month to find Maximum precipitation"
print "2.Enter Month to find aveerage and total precipitation"
print "0.quit"
ch = input()
while(ch!=0):
#start of while
if(ch==1 or ch==2):
#start of 1st if
month = input("Enter Month : ")
print " ============================== "
if(ch==1):
#start of 2nd if
max1 = -1
pos = -1
sum1 = 0
for i in xrange(l):
#start of for loop
if(data[i][1]==month):
#start of 1st if of for
if(max1<data[i][3]):
#start of inner if
max1 = data[i][3]
#end of inner if
#end of 1st if of for
sum1+=data[i][3]
pos = i
#end of for loop
if(pos==-1):
#start of if
print "No Data Available for given month"
#end of if
else:
#start of else
print "Date of Max Precipitation",format_date(data[pos][:3]),"With Max Precipitation =",max1
print "Total Amount of precipitation of given month = {:0.2f}".format(sum1)
#end of else
elif(ch==2):
#start of elif
sum = 0
n = 0
for i in xrange(l):
#start of for
if(data[i][1]==month):
#start of if
sum+=data[i][3]
n+=1
#end of if
#end of for
avg = sum/n
print "Average Precipitation of given month = {:0.2f}".format(avg)
print "Total Amount of precipitation of given month = {:0.2f}".format(sum)
#end of elif
print " ============================== "
else:
#start of else
print "Invalid Input"
#end of else
print "1.Enter Month to find Maximum precipitation"
print "2.Enter Month to find aveerage and total precipitation"
print "0.quit"
ch = input()
#end of while
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.