Question: Can someone help me with my python code? I am trying to make it reads from the file until it reaches the XXX then it
Can someone help me with my python code? I am trying to make it reads from the file until it reaches the "XXX" then it will compute the desired data (shown below). Then I need it to read from the file again (from were it previously stopped and restart the count) and display the data. Its kind of hard to explain so I will leave the desired output below. Data in the rainfall_data.txt file: 0.98 0.00 0.00 0.08 0.89 0.00 T 0.00 T 1.33 XXX 1.23 0.77 0.00 0.00 0.00 T 0.07 1.23 XXX Desired output: ************************************ Average = 3.50 days Total rainfall: 3.28 inches 0: 4 days or 44.44 % days T: 2 days or 22.22 % days 0.01: 1 days or 11.11 % days 0.1: 0 days or 0.00 % days 0.25: 0 days or 0.00 % days 0.50: 0 days or 0.00 % days 0.75: 1 days or 11.11 % days 1: 1 days or 11.11 % days 2: 0 days or 0.00 % days 3: 0 days or 0.00 % days 4: 0 days or 0.00 % days 5+: 0 days or 0.00 % days ************************************ ************************************ Average = 2.50 days Total rainfall: 3.30 inches 0: 3 days or 42.86 % days T: 1 days or 14.29 % days 0.01: 1 days or 14.29 % days 0.1: 0 days or 0.00 % days 0.25: 0 days or 0.00 % days 0.50: 0 days or 0.00 % days 0.75: 1 days or 14.29 % days 1: 1 days or 14.29 % days 2: 0 days or 0.00 % days 3: 0 days or 0.00 % days 4: 0 days or 0.00 % days 5+: 0 days or 0.00 % days ************************************ #Analyzing rainfall data a = 0 b = 0 c = 0 d = 0 e = 0 ff = 0 g = 0 h = 0 i = 0 j = 0 k = 0 m = 0 n = 0 q = 1970 r = 0 #Open rainfall data text file f = open("rainfall_data.txt", "r") #Making and array called "myArray" myArray = [] #Sorting throught the data in the file for x in f: if (x == "XXX "): break else: #For all values except "T" assign the amount to the variable "x" in the array if (x != "T "): myArray.append(float(x)) #Else if the precip value is "T" it will assign it to the value of 0.01" else: myArray.append(0.001) summ = 0.0 #Computing the sum for p in range(0, len(myArray)): summ = summ + myArray[p]; l = 0 z = 0 avg = 0 #Determining the number of consecutive days without the minimum significant rainfall for r in range(1, len(myArray)): if (myArray[r] == 0.00): a = a + 1 n = n + 1 if (myArray[r] == 0.001): m = m + 1 n = n + 1 if (myArray[r] >= 0.01 and myArray[r] <= 0.09): b = b + 1 n = n + 1 if (myArray[r] >= 0.10 and myArray[r] <= 0.24): c = c + 1 n = n + 1 if (myArray[r] >= 0.25 and myArray[r] <= 0.49): d = d + 1 n = n + 1 if (myArray[r] >= 0.50 and myArray[r] <= 0.74): e = e + 1 n = n + 1 if (myArray[r] >= 0.75 and myArray[r] <= 0.99): ff = ff + 1 n = n + 1 if (myArray[r] >= 1.00 and myArray[r] <= 1.99): g = g + 1 n = n + 1 if (myArray[r] >= 2.00 and myArray[r] <= 2.99): h = h + 1 n = n + 1 if (myArray[r] >= 3.00 and myArray[r] <= 3.99): i = i + 1 n = n + 1 if (myArray[r] >= 4.00 and myArray[r] <= 4.99): j = j + 1 n = n + 1 if (myArray[r] >= 5.00): k = k + 1 n = n + 1 #Counter if (myArray[r] < 0.10): l = l + 1 else: avg = avg + l; z = z + 1; l = 0 #Displaying the output in the terminal print(" ") print("************************************") print("Average = ", "{:.2f}".format(avg / z), " days") print("Total rainfall: ", "{:.2f}".format(summ), " inches ") print("0:", a, " days or", "{:.2f}".format((a/n)*100), "% days") print("T:", m, " days or", "{:.2f}".format((m/n)*100), "% days") print("0.01:", b, " days or", "{:.2f}".format((b/n)*100), "% days") print("0.1:", c, " days or", "{:.2f}".format((c/n)*100), "% days") print("0.25:", d, " days or", "{:.2f}".format((d/n)*100), "% days") print("0.50:", e, " days or", "{:.2f}".format((e/n)*100), "% days") print("0.75:", ff, " days or", "{:.2f}".format((ff/n)*100), "% days") print("1:", g, " days or", "{:.2f}".format((g/n)*100), "% days") print("2:", h, " days or", "{:.2f}".format((h/n)*100), "% days") print("3:", i, " days or", "{:.2f}".format((i/n)*100), "% days") print("4:", j, " days or", "{:.2f}".format((j/n)*100), "% days") print("5+:", k, " days or", "{:.2f}".format((k/n)*100), "% days") print("************************************") Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
