Skip to content

Commit e5a0eb9

Browse files
committed
Fix for wrong file
1 parent f2d223c commit e5a0eb9

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ [email protected] 4
88
99
1010
11-
11+

_sources/dictionaries/writecode.rst

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ Below is the romeo3.txt file used in Question 7.
352352

353353
The next two questions are associated with the following text file which has an email address followed by the number of messages from that address.
354354

355-
.. reveal:: mbox-short-txt-file
355+
.. reveal:: mbox-count-txt-file
356356
:showtitle: Show
357357
:hidetitle: Hide
358358

@@ -376,13 +376,13 @@ The next two questions are associated with the following text file which has an
376376
.. tab:: Question
377377

378378
.. activecode:: dict_writecode9q
379-
:datafile: mbox-short.txt
379+
:datafile: mbox-count.txt
380380
:autograde: unittest
381381

382382
Add code to the program below to figure out who has the most messages in the file. After all the data has been read and the dictionary ``message_count`` has been created, look through the dictionary using a maximum loop (see Chapter 5: Maximum and minimum loops) to find who has the most messages,
383383
and print how many messages the person has. For example, ``message_count['[email protected]']`` should be ``5``.
384384
~~~~
385-
with open("mbox-short.txt", "r") as filename:
385+
with open("mbox-count.txt", "r") as filename:
386386
message_count = {}
387387

388388
====
@@ -399,34 +399,45 @@ The next two questions are associated with the following text file which has an
399399
.. tab:: Answer
400400

401401
.. activecode:: dict_writecode9a
402-
:available_files: mbox-short.txt
402+
:available_files: mbox-count.txt
403403
:optional:
404404

405405
Add code to the program below to figure out who has the most messages in the file. After all the data has been read and the dictionary ``message_count`` has been created, look through the dictionary using a maximum loop (see Chapter 5: Maximum and minimum loops) to find who has the most messages,
406406
and print how many messages the person has. For example, ``message_count['[email protected]']`` should be ``5``.
407407
~~~~
408408
# Open file in read mode
409-
with open("mbox-short.txt", "r") as filename:
409+
with open("mbox-count.txt", "r") as filename:
410+
410411
# Create message_count dictionary
411412
message_count = {}
413+
412414
# Create variable for lines of the file
413415
messages = filename.readlines()
416+
414417
# Iterate through each message (each line)
415418
for message in messages:
416-
# Assign the key to the first (0th) element of the message
417-
key = message.split()[0]
418-
# Assign the value to the second element of the message
419-
value = message.split()[1]
420-
# Check if key is already in dictionary
421-
if key not in message_count.keys():
422-
# if not, add key/value pair to dictionary
423-
message_count[key] = value
419+
420+
# get the columns
421+
cols = message.split()
422+
423+
# Check if there are two columns
424+
if len(cols) == 2:
425+
426+
# Assign the key to the first (0th) element of the message
427+
key = cols[0]
428+
429+
# Assign the value to the second element of the message
430+
value = cols[1]
431+
432+
# add the key and value to the dictionary
433+
message_count[key] = int(value)
434+
424435
# Create variable to count emails
425436
max_emails = 0
426437
# Iterate through keys in dictionary
427438
for key in message_count.keys():
428439
# Check if key is larger than the max emails
429-
if int(message_count[key]) >= max_emails:
440+
if message_count[key] >= max_emails:
430441
# If so, reassign max_emails to that key
431442
max_emails = int(message_count[key])
432443
print(max_emails)

0 commit comments

Comments
 (0)