pickle.load(f)
will return a dictionary. If you iterate the dictionary, it yields keys, not key-values pairs.
To yield key-value paris, use items()
method (use iteritems()
method if you use Python 2.x):
for player, score in pickle.load(f).items():
print("Player: ", k, " scored : ", v)
To get multiple dictionaries out, you need to loop:
with open("high_score.dat", "r") as f:
try:
while True:
for player, score in pickle.load(f).items():
# print("Player: ", k, " scored : ", v) # k, v - typo
print("Player: ", player, " scored : ", score)
except EOFError:
pass
BTW, if you use with
statement, you don't need to close the file yourself.
# f.close # This line is not necessary. BTW, the function call is missing `()`