python 3.x - Finding the highest score from a file -


the code have designed searches highest score per level, file has multiple lines in order [playername,level,score]. used replace 0 in variable highest score per level print highest scores. however, prints 0,0,0,0,0 code is:

    current_1 = 0     current_2 = 0     current_3 = 0     current_4 = 0     current_5 = 0     fileopen = open("playerscores.txt")     filelist = fileopen.readlines()     def scoreboard  ():         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) 

this because you're not calling scoreboard, , if do, you've read file, when filelist = fileopen.readlines(), when try list(fileopen), empty list. of course that's thing, since filelist(item, 1) , filelist(item, 2) isn't going work well.

a nice way check if code doing should using logging module:

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")     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 scoreboard") print(current_1,current_2,current_3,current_4,current_5) 

however, can drastically improve readability of program:

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')     open("playerscores.txt") f:         log.info('reading scores...')         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 , score > current_1:                 log.debug('level 1 high score: %s', score)h                 current_1 = score             elif level == 2 , score > current_2:                 log.debug('level 2 high score: %s', score)                 current_2 = score             elif level == 3 , score > current_3:                 log.debug('level 3 high score: %s', score)                 current_3 = score             elif level == 4 , score > current_4:                 log.debug('level 4 high score: %s', score)                 current_4 = score             elif level == 5 , 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) 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -