2013-06-19 122 views
0

我創建一個排行榜功能爲我的比賽,但我不能讓它的工作詮釋轉換工作不

這裏是我的方法就可以了:

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 

它的作用是讀取最近的從.txt文件,並檢查高分如果球員得分越高,如果它是將其寫入新高分到文件

我的字符串轉換從highscore成INT使用int(highscore)然後和第10行我做print highscore + self.score爲一個測試但是我拋出說,我不能加海峽和一個int即使我轉換highscore爲int和我轉換self.score所以出於某種原因,轉換一個沒有工作

回答

7

int()返回一個錯誤整數,但是你放棄了那個結果。重新分配它:

highscore = int(highscore) 

功能並變化就地變量。如果self.score也是一個字符串,則需要對int(self.score)執行相同的操作,或者只刪除該行。

+0

以及即時通訊愚蠢我認爲'int'改變了變量感謝這麼多martijn! – Serial

+1

值得注意的是,如果這些值是整數,那麼後面一行'print highscore + self.score'會做出非常不同的事情,如果它們仍然是字符串的話,它們會變得非常不同。我建議使用字符串格式來適當地佈置它們,而不是串聯。 – Blckknght