Averaging Numbers From a Text file in python without using sum(). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Averaging Numbers From a Text file in python without using sum().

Here is the question ( self teaching). " Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. " I have had some help but I am not getting it. it was suggested to put the "continue" at the end of "if not" but I am not sure why. I also can't seem to sum the individual lines. I have extracted the numbers and added the counter but everything I have tried to sum then average fails.... There is nothing in the tutorials or the book that suggest how to even do this. my code : # Use the file name mbox-short.txt as the file name fname = input("Enter file name: ") fh = open(fname) count = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue count = count + 1 numbers = line.find(':') final = line[numbers+1:] print(final) So I added print(final) just to see if I have the numbers right. I get this. 0.8475 0.6178 0.6961 0.7565 0.7626 0.7556 0.7002 0.7615 0.7601 0.7605 0.6959 0.7606 0.7559 0.7605 0.6932 0.7558 0.6526 0.6948 0.6528 0.7002 0.7554 0.6956 0.6959 0.7556 0.9846 0.8509 0.9907 Please Help :(

1st Nov 2017, 6:25 PM
Aric Dunn
Aric Dunn - avatar
14 Answers
+ 9
@Aric What you did there wrong was you initialize ntot as an empty list *each time* in the loop. You have to do this before the loop begins.
1st Nov 2017, 7:10 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 8
@Aric You are getting in the right direction, as you have those numbers already! Now, instead of printing them, you might convert them to floats and add each of them to a variable. Then just divide this variable by counter and you have yourself the average without using sum() ;) BTW, this sounds familiar... Are you perhaps doing Dr. Chuck's course on coursera.org? :)
1st Nov 2017, 6:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
@Aric Introduce a new variable called num. In the loop add a line: num += float(final) Outside the loop add: print(num/counter)
1st Nov 2017, 7:06 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
No problem, man. Good luck with your course ;)
1st Nov 2017, 7:25 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
@Daniel BTW - By The Way ;)
1st Nov 2017, 7:11 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Kuba what is BTW?
1st Nov 2017, 7:10 PM
Daniel
Daniel - avatar
+ 4
Before For add finals=0 Try to add into for finals+=line[numbers+1] At the end print((finals)/(numbers+1))
1st Nov 2017, 6:59 PM
Daniel
Daniel - avatar
+ 4
file= input("enter your filename:") try: filename = open(file) readfile = filename.read() except: print("File is not found") quit() word = "X-DSPAM-Confidence:" result=0 count = 0 for line in filename: if line.startswith(word): l = len(word) number = float(line[l+1:]) result = result + number count = count + 1 continue try: avg = result/count except ZeroDivisionError: avg =0 print("Average spam confidence:", avg)
6th Jun 2020, 3:04 PM
Antima Singh
Antima Singh - avatar
+ 3
Aric the best way to check how work your program or practice is printing values in different parts of your code. In this way you will see how it works and where do you must to do changes ;-)
1st Nov 2017, 7:37 PM
Daniel
Daniel - avatar
+ 2
Ok nevermind .. had to move print outside loop. thank you the sad thing is I am not sure why what you all told me actually worked....
1st Nov 2017, 7:25 PM
Aric Dunn
Aric Dunn - avatar
+ 1
RIght I have converted to float now but I cant seem to get it to add each line. yes it is coursera :) I have been trying this but it wont sum.. just playing around with the sum even though it says not too. fname = input("Enter file name: ") fh = open(fname) count = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue count = count + 1 numbers = line.find(':') final = line[numbers+1:] ntot = list() ntot.append(float(final)) ftot = sum(ntot) print(ftot)
1st Nov 2017, 7:05 PM
Aric Dunn
Aric Dunn - avatar
+ 1
To find average : prompt = input("enter your filename:") filename = open(prompt) readfile = filename.read() word = "X-DSPAM-Confidence:" result = 0 count = 0 for line in readfile: if line.startswith(word): l = len(word) number = float(line[l+1:]) result = result + number count = count + 1 continue avg = result/count print(Average Spam Confidence:", avg)
7th May 2020, 9:57 AM
Pranitha
0
fname = input("Enter file name: ") fh = open(fname) count = 0 sum = 0 for line in fh: if line.startswith('X-DSPAM-Confidence:'): count = count+1 num = float(line[line.find(":")+1:]) sum= sum+num print('Average spam confidence:', sum / count)
22nd May 2020, 6:16 AM
Mariyam
Mariyam - avatar
- 1
Wont convert num += float(final) nevermind figured it out.. TY ! well except it printed everything 0.8475 0.73265 0.7204666666666667 0.729475 0.7361 0.73935 0.7337571428571428 0.7372249999999999 0.7397666666666667 0.74184 0.7376636363636364 0.739575 0.7408307692307693 0.7422357142857143 0.7389666666666668 0.7400187500000002 0.7348764705882355 0.7326500000000001 0.7284473684210527 0.7270350000000001 0.7283857142857144 0.7268954545454547 0.7255478260869567 0.7268000000000002 0.7371120000000002 0.7414884615384617 0.7507185185185187
1st Nov 2017, 7:18 PM
Aric Dunn
Aric Dunn - avatar