如何在文本文件中添加和排序值以创建排名前5位的排行榜Python

时间:2019-03-27 18:12:35

标签: python python-3.x

我已经测试了下面显示的代码(使用变量),并且当我查看它(使用记事本)时,文本文件显示'answer:NONE'。请您仔细阅读这段代码,指出我的错误,非常感谢

这是在Python中,并使用变量对其进行测试 例如:points = 10name = 'Alex'

#Creating text file to save scores
f = open('scores.txt','w')
a = print(name, points)
f.write('answer:'+str(a))
f.close()

#ordering scores in descending order 
scores = []
with open("scores.txt") as f:
for line in f:
        name, score = line.split(',')
        score = int(points)
        scores.append((name, score))

        scores.sort(key=lambda s: s[1])

        for name, score in scores:
          print(name, score)
      sys.exit("Best luck nest time, your scores have been        recorded")

0 个答案:

没有答案