Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework question
#3
I guess it did not find the string or fh is empty.
Instead of summing the total confidence and counting the amount, you can append to a list or use a generator.
The list knows it's own size. So if you have the list, you have all information you need. The values and the size.
If the list is empty, then don't do the calculation. This safes you against dividing by 0.

import statistics
import io

# just to get the data online
import requests


def get_confidence_avg(fd):
    """
    fd should be a file-like object in text mode
    or an iterable with str (lines) as elements 
    """
    for line in fd:
        if line.startswith('X-DSPAM-Confidence:'):
            yield float(line.split(':')[1])


mail_fd = io.StringIO(requests.get('https://www.py4e.com/code3/mbox-short.txt').text)
# mail_fd is now a file like object and holds the data from the example
# you can use
# with open(somefile) as fd:
#     confidences = list(get_confidence_avg(fd))

confidences = list(get_confidence_avg(mail_fd)) # populating the list with confidence

if confidences:
    mean = statistics.mean(confidences)
    median = statistics.median(confidences)
    # or calculating the mean manually
    mean_manual = sum(confidences) / len(confidences)
    print('Mean / Mean manual / Median')
    print(mean, mean_manual, median)
else:
    print('Did not found any match. No information about confidence.')
PS: Normally open().read() should work and the fd should be garbage collected afterwards. To be explicit is better. Just use a context-manager or use the modern pathlib. The Path object has the methods read_text and read_bytes. Both methods are using a context manager under the hood.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Homework question - by dmhhfm - Apr-09-2019, 12:43 PM
RE: Homework question - by Gribouillis - Apr-09-2019, 01:10 PM
RE: Homework question - by DeaD_EyE - Apr-09-2019, 01:35 PM
RE: Homework question - by dmhhfm - Apr-10-2019, 01:51 AM
RE: Homework question - by DeaD_EyE - Apr-10-2019, 07:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework help:While Loops question Midhat_School 6 3,219 Jul-26-2020, 10:23 AM
Last Post: pyzyx3qwerty
  IF ELSE Homework Question buckthimble 1 2,205 Mar-29-2020, 06:29 AM
Last Post: buran
  Python Homework Question OrcDroid123 1 2,427 Sep-01-2019, 08:44 AM
Last Post: buran
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,790 Nov-02-2018, 01:06 PM
Last Post: buran
  yet another homework question HakolYahol 16 7,884 Sep-27-2018, 04:52 PM
Last Post: gruntfutuk
  python in JES homework question, lsteffen 1 3,058 Feb-11-2018, 05:52 PM
Last Post: Taco_Town

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020