Letters in the Letterbox: else not executing?

Here is the question I have been asked to answer (in python):

You live in a big apartment block, and sorting mail and parcels is quite a complex task. Write a program that reads in a file with all the deliveries, asks for the user’s name and prints out a summary of all the mail.

Your program should read in a file named mail.txt that looks like this:

Jane Fairfax,Letter
Frank Churchill,Letter
Emma Woodhouse,Letter
Frank Churchill,Letter
Harriet Smith,Package
Emma Woodhouse,Letter
Philip Elton,Package
Emma Woodhouse,Package

Your program should work like this:

Name: Emma Woodhouse
2 Letters
1 Package

Here is another example:

Name: Jane Fairfax
1 Letter
No Packages

If the person doesn’t have any mail, your program should work like this:

Name: Elizabeth Bennet
No mail

Here is my solution so far:

import csv

dictionary = {}

name = input('Name: ')

with open('mail.txt', newline='') as f:
  reader = csv.reader(f)
  for line in reader:
    key, value = line[0], line[1]
    if key in dictionary:
      dictionary[key].append(value)
    else:
      dictionary[key] = [value]
    
for person, items in dictionary.items():
  lettercount = 0
  packagecount = 0
  if person == name:
    for item in items:
      if item == 'Letter':
        lettercount += 1
      else:
        packagecount += 1

    l = ''
    p = ''
    if lettercount == 0:
      l = 'No Letters'
    elif packagecount == 0:
      p = 'No Packages'
    elif lettercount == 1:   
      l = '1 Letter'
    elif packagecount == 1:
      p = '1 Package'
    elif packagecount == 0 and lettercount == 0:
      print('No mail')
    else:
      l = f'{lettercount} Letters'
      p = f'{packagecount} Packages'
      print('e')
      
    print(l)
    print(p)

This is the output of my code, given the name Emma Woodhouse. No matter the name, if they have no or 1 package/letter it works, but as soon as there is more than 1 it doesn’t work.

Name: Emma Woodhouse

1 Package

I have determined the issue must be here as I cannot see anything wrong with the previous variables.

    if lettercount == 0:
      l = 'No Letters'
    elif packagecount == 0:
      p = 'No Packages'
    elif lettercount == 1:   
      l = '1 Letter'
    elif packagecount == 1:
      p = '1 Package'
    elif packagecount == 0 and lettercount == 0:
      print('No mail')
    else:
      l = f'{lettercount} Letters'
      p = f'{packagecount} Packages'
      print('e')

For some reason, the else statement will not trigger, as I tested by asking it to print e. I have tried basically everything up to this point. I have asked chatgpt in about every way possible (to no avail), tried changing l and p only under the else statement, tried using elif statements such as

elif lettercount > 1: 
  l = f'{lettercount} Letters'

and seeing if that gets executed (it doesnt), I even tried using different data types. I am at a complete dead end. Can someone please help?

>Solution :

This code is the problem:

if lettercount == 0:
  l = 'No Letters'
elif packagecount == 0:
  p = 'No Packages'
elif lettercount == 1:
  l = '1 Letter'
elif packagecount == 1:
  p = '1 Package'
elif packagecount == 0 and lettercount == 0:
  print('No mail')
else:
  l = f'{lettercount} Letters'
  p = f'{packagecount} Packages'

You’re using a single if/elif/else structure, so it will only execute one of the code blocks.

In the sample data, Emma Woodhouse does have one package, so the elif packagecount == 1 block is executed. The final else block is not executed.

Rearrange your code to handle letters and packages separately:

if lettercount == 0:
    ...
elif lettercount  == 1:
    ...
else:
    ...

if packagecount == 0:
    ...
elif packagecount == 1:
    ...
else:
    ...

Leave a Reply