17

I'm a beginner programmer and am working through the book python for the absolute beginner. I have come across a problem trying to write a high scoring function for the trivia game. when the function 'highscore(user, highscore):' is called on I try to assign the arguments accordingly so I can pickle the information to a file for later use. however I am running into an error trying to dump the info needed.

def highscore(user, highscore):
    '''stores the players score to a file.'''
    import pickle, shelve
    user = ''
    highscore = 0
    #Hscore = shelve.open('highscore.dat', 'c')
    Hscore = open('highscore.txt', 'a')
    pickle.dump(user, Hscore)
    pickle.dump(highscore, Hscore)
    #Hscore.sync()
    Hscore.close()

since I'm working through the book and have also seen shelves in action I tried using them too but run into their own set of errors. so ignore the '#'s at this time.

at the part pickle.dump is where I'm generating an error. I keep getting (as the title suggests) a write argument error.

I don't understand why its not recognizing them as string. as when they are defined in the main function it is indeed a string..

Austin Howard
  • 780
  • 4
  • 10
  • 24

1 Answers1

23

Looks like you are working through a book aimed at Python 2. You need to open your file in binary mode; add b to the mode:

Hscore = open('highscore.txt', 'ab')

If your book contains more issues like these, it may be time to switch to one that supports Python 3 or to install Python 2.7 at least for the purposes of completing the book exercises.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343