Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 12:52
Show Gist options
  • Save MichelleDalalJian/15dad18def33b4124659aa89a5af5c98 to your computer and use it in GitHub Desktop.
Save MichelleDalalJian/15dad18def33b4124659aa89a5af5c98 to your computer and use it in GitHub Desktop.
7.2 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 …
# Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fhand = open(fname)
count = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
count = count + 1
total = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:"):
line = float(line[21:])
total = line + total
average = total/ count
print("Average spam confidence:",average)
@Ujjwal421
Copy link

What is total = line + total do

@imohammedmq
Copy link

Try this code easy and simpe

fname = input("Enter file name: ")
fhand = open(fname)

total = 0
count = 0

for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
line = float(line[21:])
total = line + total
count = count + 1

average = total/ count

print("Average spam confidence:", average)

Note: use the file name mbox-short.txt as the file name

@awkwardturtle1
Copy link

SIMPLE AND SHORT

fname = input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:") :
line = float(line[21:])
total = line + total
count = count + 1
average = (total / count)
print("Average spam confidence: " + str(average) )

@sartorirafa
Copy link

# Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    parts = line.split()
    total = float(parts[1]) + total
    count = count + 1
        

print("Average spam confidence:", total/count)

@IamPrajwalDubey
Copy link

Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ")
fh = open(fname)
tot = 0
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count+=1
line = float(line[21:])
tot=tot+line
average = tot/count
print("Average spam confidence:",average)

@Boyue-Z
Copy link

Boyue-Z commented Apr 4, 2021

Use mnemonic variable names and a function.

# Use the file name mbox-short.txt as the file name
def getValue(sLine):
    c = sLine.find(':')
    return float(sLine[c+1:])

fname = input("Enter file name: ")
fh = open(fname)
fSum = 0
nCount = 0

for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    fValue = getValue(line)
    fSum = fSum + fValue
    nCount = nCount + 1
    # print ("Each:", fValue, fSum, nCount)

print("Average spam confidence:", fSum/nCount)

@JusticeSelormBruce
Copy link

JusticeSelormBruce commented Apr 22, 2021

solution

fname = input("Enter file name: ")
fh = open(fname)
counter =0
result =0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
index = line.find(":")
value = line[index+1:len(line)]
number = value.strip()
float_number =float(number)
result = result + float_number
counter = counter + 1
print("Average spam confidence: {}".format(result/counter))

@snehaguptaa
Copy link

Easiest answer:
fil = open('C:\Users\admin\OneDrive\Desktop\mbox-short.txt','r')
cnt = 0
a=0
for ch in fil:
if not ch.startswith('X-DSPAM-Confidence:'):
continue
print(ch.rstrip())
cnt+=1
i=ch[20:]
a+=float(i)
print("Average spam Confidence:",a/cnt)

@SayanDgp
Copy link

Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ")
fhand = open(fname)

count = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
count = count + 1

total = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:"):
line = float(line[21:])
total = line + total

average = total/ count

print("Average spam confidence:",average)

In the above code the logic in the line i couldn't got can anyone explain me please?
line = float(line[21:])
total = line + total

how 21 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment