I am currently testing the third phase of my program, and this errors repeatedly appears throughout the testing of this code below:
I am attempting to read in some values into a dictionary and printing the dictionary to show me the last three scores of each user (I originally chose 4, but I have changed my design idea.) The code is below:
import collections
from collections import defaultdict
worker_scores = collections.defaultdict(lambda: collections.deque(maxlen=3))
import operator
with open('score_names.txt') as f:
for line in f:
worker,score = line.split(":")
worker_scores[worker].append(int(score))
for name in sorted(program_scores):
print(name," ".join(map(str,program_scores[worker])))
The text file is also shown below:
AdamJohnson:10
AdamJohnson:20
AdamJohnson:30
AdamJohnson:40
AdamJohnson:50
AdamJohnson:60
MichaelJordan:70
MichaelJordan:80
MichaelJordan:90
MichaelJordan:100
DavidSnowman:110
DavidSnowman:120
DavidSnowman:130
DavidSnowman:140
Now, if the text file only included AdamJohnson, as shown:
AdamJohnson:10
AdamJohnson:20
AdamJohnson:30
AdamJohnson:40
AdamJohnson:50
AdamJohnson:60
Then the IDLE correctly prints:
AdamJohnson 40 50 60
BUT when MichaelJordan and DavidSnowman exist, I want the IDLE TO show:
AdamJohnson 40 50 60
MichaelJordan 80 90 100
MichaelJordan 120 130 140
Yet instead, this error appears:
Traceback (most recent call last):
File "/Users/Ahmad/Desktop/TESTS/Task 3/Dictionary copy.py", line 8, in <module>
worker,score = line.split(":")
ValueError: need more than 1 value to unpack
How can I fix this to make sure that the result I want is obtained in the IDLE when printed?
Thanks, Delbert.
UPDATE The code now functions, but the scores are printed incorrectly... see this code:
AdamJohnson 120 130 140
DavidSnowman 120 130 140
MichaelJordan 120 130 140
Why is this happening???