0

テキスト ファイルに保存された特定のユーザーからのスコアを 3 つだけ許可するようにコードを設定しました。しかし、私はこれを機能させるのに苦労しています。pname は個人名の変数で、正解した金額は変数 correct の下に格納されます。また、変数 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