0

I'm trying to extract the values inside a file so I can make an average.

fname = input("Enter file name: ")
fh = open("mbox-short.txt")
count = 0
total = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:"):
        continue
    a = line.find(":")
    line = line.strip()
    b = float((line.find[a + 1]))
    count = count + 1
    total = b + total
    print(total/count)

I can't use the function sum. I try to use slice after the : and the output is wrong.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

When you call line.find(':') you end up with an numeric value for where that character exists in the string. You don't want to call .find() again to get the content after that character, you want to slice the string. b = float(line[a+1:]).

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • wow!! thank you so much really. im just starting to code and this code gave me a lot of headache. thank you so much! – Fabian Vagi Oct 26 '20 at 16:56