0

I'm creating a highscore feature for my game but I cant get it to work

here is my method for it:

def game_over(self):
    # Game over Screen
    keys = pygame.key.get_pressed()
    self.gameover = pygame.image.load('resources/screen/game_over.png')
    screen.blit(self.gameover,(0,0))

    high_filer = open('highscores.txt', 'r')
    highscore = high_filer.read()
    high_filer.close()
    int(highscore)
    int(self.score)
    print highscore + self.score

    if self.score > highscore: 
        high_filew = open('highscores.txt', 'w')
        high_filew.write(str(self.score))
        high_filew.close()

    if (keys[K_RETURN]):
        self.state = 1

What it does is reads the most recent highscore from a .txt file and checks if the players score is higher if it is it writes the new highscore into the file

I convert the string from highscore into a int by using int(highscore) then and on line 10 I do print highscore + self.score as a test but I throws an error that says that I can't add a str and an int even though I converted highscore to an int and I converted self.score so for some reason one of the conversions didn't work

sloth
  • 99,095
  • 21
  • 171
  • 219
Serial
  • 7,925
  • 13
  • 52
  • 71

1 Answers1

7

int() returns an integer, but you discard that result. Reassign it:

highscore = int(highscore)

The function does not change the variable in-place. If self.score is a string as well, you'll need to do the same thing for int(self.score), or just remove that line.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • well im dumb i thought that that `int` changed the variable thanks so much martijn ! – Serial Jun 19 '13 at 22:21
  • 1
    It's worth noting that the later line, `print highscore + self.score` will do something very different if those values are integers than it will if they are still strings. I'd suggest using string formatting to lay them out appropriately, rather than concatenation. – Blckknght Jun 19 '13 at 23:52