You may define the maximum number of attempts made, say N. Then if the user guesses correctly in M guesses, then assign (N-M) to score. Here, better the guessing power, higher will be the value of (N-M).
I am just confused as to how to write the code to print to a txt file to save the scores.
I don't think you will need to print the txt file. You just need to save 5 high scores and the respective usernames in a txt file. Then, whenever a user scores higher than the lowest high score, update the high score list by making appropriate changes in the txt file.
Hope this helps.