You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sources/dictionaries/writecode.rst
+25-14Lines changed: 25 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -352,7 +352,7 @@ Below is the romeo3.txt file used in Question 7.
352
352
353
353
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.
354
354
355
-
.. reveal:: mbox-short-txt-file
355
+
.. reveal:: mbox-count-txt-file
356
356
:showtitle: Show
357
357
:hidetitle: Hide
358
358
@@ -376,13 +376,13 @@ The next two questions are associated with the following text file which has an
376
376
.. tab:: Question
377
377
378
378
.. activecode:: dict_writecode9q
379
-
:datafile: mbox-short.txt
379
+
:datafile: mbox-count.txt
380
380
:autograde: unittest
381
381
382
382
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,
383
383
and print how many messages the person has. For example, ``message_count['[email protected]']`` should be ``5``.
384
384
~~~~
385
-
with open("mbox-short.txt", "r") as filename:
385
+
with open("mbox-count.txt", "r") as filename:
386
386
message_count = {}
387
387
388
388
====
@@ -399,34 +399,45 @@ The next two questions are associated with the following text file which has an
399
399
.. tab:: Answer
400
400
401
401
.. activecode:: dict_writecode9a
402
-
:available_files: mbox-short.txt
402
+
:available_files: mbox-count.txt
403
403
:optional:
404
404
405
405
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,
406
406
and print how many messages the person has. For example, ``message_count['[email protected]']`` should be ``5``.
407
407
~~~~
408
408
# Open file in read mode
409
-
with open("mbox-short.txt", "r") as filename:
409
+
with open("mbox-count.txt", "r") as filename:
410
+
410
411
# Create message_count dictionary
411
412
message_count = {}
413
+
412
414
# Create variable for lines of the file
413
415
messages = filename.readlines()
416
+
414
417
# Iterate through each message (each line)
415
418
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
0 commit comments