2015-04-01 71 views
-2

我設計的代碼搜索每個關卡的最高分數,該文件有多行,順序爲[playername,level,score]。我用這個來取代每個級別得分最高的變量中的0,然後打印出最高分數。然而,它只是打印0,0,0,0,0 的代碼是:查找文件中的最高分

current_1 = 0 
    current_2 = 0 
    current_3 = 0 
    current_4 = 0 
    current_5 = 0 
    FileOpen = open("playerScores.txt") 
    FileList = FileOpen.readlines() 
    def ScoreBoard (): 
     for item in list(FileOpen): 
      CheckLevel = FileList(item,1) 
      CheckScore = Filelist(item,2) 
      if CheckLevel == 1: 
       if CheckScore > current_1: 
        current_1 = CheckScore    
      elif CheckLevel == 2: 
       if CheckScore > current_2: 
        current_2 = CheckScore 
      elif CheckLevel == 3: 
       if CheckScore > current_3: 
        current_3 = CheckScore 
      elif CheckLevel == 4: 
       if CheckScore > current_4: 
        current_4 = CheckScore 
      elif CheckLevel == 5: 
       if CheckScore > current_4: 
        current_5 = CheckScore 

    ScoreBoard 
    print(current_1,current_2,current_3,current_4,current_5) 
+0

可能不會解決你的問題,但是如果你使用'&&'連接它們,每個'if checkLevel ...''如果checkScore ...'對可以合併爲1行。將每個級別的當前高分存儲在列表中以減少所有重複的代碼也可能是有利的。 – Carcigenicate 2015-04-01 11:02:37

+0

謝謝:)我現在試試吧 – user3921501 2015-04-01 11:15:27

回答

0

有(至少)三個問題與您的代碼:

  • 你從來沒有真正打電話給你ScoreBoard方法(缺少()
  • 創建新的本地變量current_X而不是使用那些global
  • 從文件讀取所有行之後,FileOpen迭代器疲憊
  • 不知道什麼FileList(item,1)應該是,但它不會給你第二個項目在該行

你可以嘗試這樣的:

def ScoreBoard (): 
    global current_1, current_2, ..., current_5 
    for item in FileList: # read from the lines, not from the file 
     CheckLevel = item.split(",")[1] 
     # ... more code 
ScoreBoard() # mind the() 

還有一些更你可能想要改進的東西:

  • 你把所有玩家的分數放到相同的變量中;也許添加播放器的名稱作爲參數?
  • ,而不是使用單個變量對每個分數,使用字典
  • 而不是使用全局變量,返回字典從功能
  • 使用with打開文件

事情是這樣的:

def scoreboard(name): 

    all_scores = {} 
    with open("playerScores.txt") as f: 
     for line in f: 
      player, level, score = line.split(",") 
      if player == name and score > all_scores.get(level, 0): 
       all_scores[level] = int(score) 
    return all_scores 

current = scoreboard("name of some player") 
print(current) 
+0

我做了兩個修復,但輸出仍然是0,0,0,0,0 – user3921501 2015-04-01 11:17:40

+0

非常感謝!這使它的工作:) – user3921501 2015-04-01 11:35:40

0

這是因爲你不打電話給ScoreBoard,即使你這樣做,你已經讀了你的文件,當你做FileList = FileOpen.readlines(),所以當你試圖做list(FileOpen)時,你會得到一個空的列表。當然這是件好事,因爲FileList(item, 1)FileList(item, 2)不會很好地工作。

一個很好的方法來檢查,如果你的代碼是做什麼的應該是通過使用記錄模塊的內容:

import logging 

log = logging.basicConfig(level=logging.DEBUG) 


current_1 = 0 
current_2 = 0 
current_3 = 0 
current_4 = 0 
current_5 = 0 

FileOpen = open("playerScores.txt") 
FileList = FileOpen.readlines() 

def ScoreBoard (): 
    log.debug(">> ScoreBoard") 
    for item in list(FileOpen): 
     log.debug("Checking item %s", item) 
     CheckLevel = FileList(item,1) 
     CheckScore = Filelist(item,2) 
     if CheckLevel == 1: 
      if CheckScore > current_1: 
       current_1 = CheckScore    
     elif CheckLevel == 2: 
      if CheckScore > current_2: 
       current_2 = CheckScore 
     elif CheckLevel == 3: 
      if CheckScore > current_3: 
       current_3 = CheckScore 
     elif CheckLevel == 4: 
      if CheckScore > current_4: 
       current_4 = CheckScore 
     elif CheckLevel == 5: 
      if CheckScore > current_4: 
       current_5 = CheckScore 

log.debug("Calling ScoreBoard") 
ScoreBoard 
log.debug("Done with ScoreBoard") 
print(current_1,current_2,current_3,current_4,current_5) 

但是,您可以大大提高你的程序的可讀性:

import logging 

log = logging.basicConfig(level=logging.DEBUG) 

current_1 = 0 
current_2 = 0 
current_3 = 0 
current_4 = 0 
current_5 = 0 


def scoreboard(): 
    log.debug('>> scoreboard') 
    with open("playerScores.txt") as f: 
     log.info('Reading scores...') 
     for line in f: 
      level, score = line.strip().split() 
      level = int(level) 
      score = int(score) 
      log.debug('level: %s score: %s', level, score) 

      if level == 1 and score > current_1: 
       log.debug('Level 1 high score: %s', score)h 
       current_1 = score 
      elif level == 2 and score > current_2: 
       log.debug('Level 2 high score: %s', score) 
       current_2 = score 
      elif level == 3 and score > current_3: 
       log.debug('Level 3 high score: %s', score) 
       current_3 = score 
      elif level == 4 and score > current_4: 
       log.debug('Level 4 high score: %s', score) 
       current_4 = score 
      elif level == 5 and score > current_5: 
       log.debug('Level 6 high score: %s', score) 
       current_5 = score 

    log.debug("<< scoreboard") 


scoreboard() 
print(current_1,current_2,current_3,current_4,current_5) 
+0

非常感謝:)現在我會嘗試 – user3921501 2015-04-01 11:37:30