0

我有一个代码设置为只允许保存在文本文件中的特定用户的 3 个分数。但我正在努力完成这项工作。pname 是人名的变量,他们正确的数量存储在变量正确的下面。我还试图添加他们使用变量 etime 所花费的时间。我有基础,但无法修复错误或使这项工作,因为我试图将其从另一个答案调整到另一个问题。谢谢你。

                SCORE_FILENAME  = "Class1.txt"
                MAX_SCORES = 3

                try: scoresFile = open(SCORE_FILENAME, "r+")
                except IOError: scoresFile = open(SCORE_FILENAME, "w+") # File not exists
                actualScoresTable = []
                for line in scoresFile:
                    tmp = line.strip().replace("\n","").split(",")
                    actualScoresTable.append({
                                            "name": tmp[0],
                                            "scores": tmp[1:],
                                            })
                scoresFile.close()

                new = True
                for index, record in enumerate( actualScoresTable ):
                    if record["name"] == pname:
                        actualScoresTable[index]["scores"].append(correct)
                        if len(record["scores"]) > MAX_SCORES:
                            actualScoresTable[index]["scores"].pop(0) # OR del actualScoresTable[index]["scores"][0]
                        new = False
                        break
                if new:
                    actualScoresTable.append({
                                             "name": pname,
                                             "scores": correct,
                                             })

                scoresFile = open(SCORE_FILENAME, "w+") # Truncating file (write all again)
                for record in actualScoresTable:
                    scoresFile.write( "%s,%s\n" % (record["name"], ","(record["scores"])) )
                scoresFile.close()
4

1 回答 1

0

首先,您在将分数写入文件时遇到问题:

...
scoresFile.write( "%s,%s\n" % (record["name"], ","(record["scores"])) )
...

由于","(record["scores]). 为了解决这个问题,只需删除","似乎是错字的 。

之后,您在覆盖当前分数时出现语义错误。一方面,您将已输入的分数读取为字符串:

...
tmp = line.strip().replace("\n","").split(",")
actualScoresTable.append({
                        "name": tmp[0],
                        "scores": tmp[1:],
                        })
...

此外,name,score1,score2,...您最终不会以 format 形式编写分数,而是将其编写为,name,[score1, score2]因为您正在编写原始列表对象,也在以下行中:

...
scoresFile.write( "%s,%s\n" % (record["name"], ","(record["scores"])) )
...


接下来,要解决导致程序错误输出分数的问题,您必须进行一些更改。一方面,您必须确保从文件中获取分数时,将它们更改为整数。

...
for line in scoresFile:
    tmp = line.strip().replace("\n","").split(",")

    # This block changes all of the scores in `tmp` to int's instead of str's
    for index, score in enumerate(tmp[1:]):
        tmp[1+index] = int(score) 

    actualScoresTable.append({
                            "name": tmp[0],
                            "scores": tmp[1:],
                            })
...

之后,您还必须确保在创建新条目时,即使只有一个分数,您也将其存储在列表中:

...
if new:
    actualScoresTable.append({
                             "name": pname,
                             "scores": [correct], # This makes sure it's in a list
                             })
...

最后,为了确保程序以正确的格式输出分数,您必须将它们转换为字符串并在它们之间放置逗号:

...
for record in actualScoresTable:

    for index, score in enumerate(record["scores"]):
        record["scores"][index] = str(score)

    # Run up `help(str.join)` for more information
    scoresFile.write( "%s,%s\n" % (record["name"], ",".join(record["scores"])) )
...


这应该这样做。让我知道如果有什么不起作用!

于 2015-02-11T13:29:19.967 回答