我设计的代码搜索每个级别的最高分数,该文件有多行,分别是[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 :(得分: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)
答案 1 :(得分: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)