0

This a problem in my textbook, where you create a program that makes a histogram of the emails in the text file (which they are always the second word).

handle = open("mbox-short.txt")

senders = {}
for line in handle:
    if line.startswith('From'):
        emails = line.split()
        if len(emails) < 3: continue
        email = emails[1]
        senders[email] = senders.get(email , 0) + 1

bigsender = None 
bigcount = None
for sender,times in senders.items():
    if bigcount < times:
        bigcount = times
        bigsender = sender    

print(bigsender, bigcount)

But when I run it produces an error which says:

TypeError: '<' not supported between instances of 'NoneType' and 'int'

Yet when I change the last condition to:

if bigcount is None or bigcount < times:

Aren't we still comparing bigcount to times, I don't get what did change?

This is text file: https://www.py4e.com/code3/mbox-short.txt

Anttarm
  • 5
  • 2
  • 4
    If `bigcount` is `None`, then the condition `bigcount is None or bigcount < times` evaluates to true without checking `bigcount < times`. See [Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation). – khelwood Jan 22 '20 at 14:24
  • 2
    you could just initiate `bigcount = 0` (or `float('-inf')` if you must) – Chris_Rands Jan 22 '20 at 14:28
  • See [Does Python support short-circuiting?](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) – khelwood Jan 22 '20 at 14:32
  • @Chris_Rands then I can use any negative number, right? since the count is always positive – Anttarm Jan 22 '20 at 14:37

1 Answers1

0

When you write if bigcount is None or bigcount < times you have two separate conditions: bigcount is None and bigcount < times. If we go in order, bigcount is None is evaluated as true. Because there is no possible value that bigcount < times can take that can make this entire line false (because true or some_boolean is always true for any possible value of some_boolean), the evaluation short circuits and doesn't evaluate the second statement, so never gets a chance to produce the type error.

In other words, bigcount < times produces an error, because you can't compare bigcount and times, but in your second example that bit of code is never evaluated.