2015-02-09 66 views
0

我有一个代码设置为只允许来自特定用户的3个分数保存在文本文件中。但我正在努力完成这项工作。 pname是人员名称的变量,他们正确的数量存储在正确的变量下。我也试图增加他们使用变量etime所花费的时间。我有基础,但无法修复错误或使这项工作,因为我试图从另一个答案适应不同的问题。 谢谢。所有的Python保存到有限制的文件

   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() 
+1

我认为你的问题会受益于简化。 – 2015-02-09 12:14:44

+1

这是用于[GCSE计算编程任务(14 - 16岁)](http://www.reddit.com/r/Python/comments/2gawvg/gcse_computing_programming_tasks_14_16_year_olds/)? – 2015-02-09 12:16:35

+0

它是我班上唯一一个这样做的。并且是该国6%的一部分。我挣扎着。即使我的老师也无法帮助我,因为他们对Python也不太了解。那么有人可以帮忙吗?我只是试图让它保存变量:pname,正确和etime(人名,他们得到的权利和他们花费的时间),所以我想它看起来像这样: – PythonBeginner 2015-02-09 13:20:38

回答

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,...写作分数此外,你写出来它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"]))) 
... 


这应该做它。让我知道,如果有什么不工作!

+0

嘿,感谢您的帮助,一切正常,因为它应该,但我无法得到我的分数输出和排序等。有没有我能够直接与您联系的地方?谢谢 :) – PythonBeginner 2015-02-13 14:14:03