0


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???

1 Answer 1

1

When you print the scores at the end, you're looking them up via the variable worker which doesn't change in your loop, rather than name.

Sign up to request clarification or add additional context in comments.

1 Comment

So it would be for name in sorted(program_scores): print(name," ".join(map(str,program_scores[worker])))? I'm guessing that's right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.