I am working on a Python problem for an online course. I need to extract floats from a .txt file and find the average of them; however, when I print the length from the floats it gives me 7. The length of the floats is more than 7. This is messing with my code and returning the wrong value. Would someone look at my code and tell me why it returns a length of 7? Where is my mistake?
I´m posting my code and all the floats that return when I print only the floats below.
fname = input("Enter file name: ")
fh = open("mbox-short.txt")
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
num = line.find(".")
numstr = line[num-1:]
sum = 0
print(float(numstr))
el = (float(numstr))
if sum < el:
sum+=el
length = len(numstr)
res = sum/length
print("Average spam confidence: ", res)
Have you tried to print the string that’s giving such length? Try printing it, but with added some specific characters at the start and ending, to delimit the actual start and ending. There might be some characters (like spaces or new line characters) that might not be so easy to notice.
I ran the .split() to get rid of white space print function
print(numstr.split())
It returned this and the length was still 7. I also ran rsplit(). Same return. I don´t understand why the len() is returning something less than what is.
I want to make sure I understand where I´m at and what I need to do from here.
WHERE I´M AT:
So far my code prints takes strings of floats (that are crowded with /n) from the .txt (Code below)
fname = input("Enter file name: ")
fh = open("mbox-short.txt")
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
num = line.find(".")
numstr = line[num-1:]
WHAT I NEED TO DO:
First I need to find a way to iterate through the string floats being collected from the .txt file, eliminate the whitespace and convert to a float and, when done with that, set up a counter to count the converted float .
Second, I need to iterate through the converted floats and then add each one to the previous one, saving the total sum in a variable and dividing that by the counter for the floats.
I am testing my code to see if I am accumlating the floats and the counter.
#Assigning file name user inputs to fname
fname = input("Enter file name: ")
#assigning action of opening file to fh
fh = open(fname)
#looping through fh
for line in fh:
#for every line strip off the white space
line = line.rstrip()
#initialize counter and sum for accum. tot sum of floats and number of float instances
counter = 0
sum = 0
#Include exception to insure loop focuses on specific instance of float
if not line.startswith("X-DSPAM-Confidence:"):
continue
#identify decimal point
num = line.find("0.")
try:
#use try to test if value is float through attempting to convert to float
numstr = float(line[num:])
#conversion is successful add current sum
sum = sum + numstr
#note occurrence of float by adding to counter
counter = counter + 1
#if try fails return top of loop and run again
except:
continue
print(counter)
print(sum)
I know this means my counter is in the wrong place…as well as my sum variable…
but I don´t get it. I put them in try because if try is successful (and it is a float) they´ll add one to counter.
This is incorrect. My logic is that if try completes successively during an instance of the loop than add one to counter and add the sum to the current converted value in the loop and ascribe that new value to the variable sum.