2014-09-28 2 views
0

Я пытаюсь написать программу, которая записывает последние три попытки (оценки) теста. Пользователь может повторно протестировать тест в любой момент, поэтому программа должна найти, существует ли пользователь и добавить последнюю оценку пользователей. Если пользователь не существует, он должен записывать имя пользователя вместе с попытками пользователей. Мне удалось заставить программу/функцию записать первый балл пользователя, но я стараюсь добавить дальнейшие попытки, если пользователь существует. Я также не знаю, как остановить список от роста - он должен записывать только последние три попытки - дальнейшие попытки должны переписать поверх существующих. Пожалуйста, извините мое невежество, если это неправильно - я учусь.Добавить в список данных написать в CSV

Мне нужен выход, чтобы выглядеть следующим образом: (после 3-х попыток список должен начать перезапись себя

Фред, 18,17,16

Павла, 15,12,16

В код не делает именно то, что я хочу, но единственная ошибка, которую я получаю, - это когда пользователь уже существует: scoresFile.write (строка + «\ n») ValueError: операция ввода-вывода в закрытом файле

#add score 
def main(): 
    #ask for name and score 
    name = input("Please enter the name you wish to add") 
    score = input("Please enter the high score") 
    message="" 

    #open the highscores line and read in all the lines to a list called ScoresList. Then close the file. 
    scoresFile = open("highscores.txt","r") 
    ScoresList = scoresFile.readlines() 
    scoresFile.close() 

    #for each line in the ScoresList list 
    for i in range(0, len(ScoresList)): 

     #check to see if the name is in the line 
     if name in ScoresList[i]: 

      #append the score to the end of the list 
      ScoresList[i] = (name + (str(score) + ",")) 

      #write the scores back to the file. Overwrite with the new list 
      scoresFile = open("highscores.txt","w") 
      for line in ScoresList: 
       scoresFile.write(line + "\n") 
       scoresFile.close() 

      #no need to continue in the loop so break out. 
      #break 
      else: 
       # message as user does not exist 
       message = "" 

#if message is still blank then it means that the name was not found. Open the 
#file for appending and add the new name and score to the end. 
    if message=="": 
     message = "New score added." 
     scoresFile = open("highscores.txt","a") 
     scoresFile.write(name + str(score)+"\n") 
     scoresFile.close() 


    print(message) 

main() 
+0

Пожалуйста [править] ваш вопрос, чтобы включить образец ввода, в вывода, который вы хотите получить, и любых ошибок или трассировок. – MattDMo

ответ

1

Одно самоходные объяснили решение вашей проблемы:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

# name,score_1,score_2,score_3 
# append score to a user 
# |_ if user not exists: add user and new score 
# |_ an user can't have more than 3 scores 


SCORE_FILENAME = "highscores.txt" 
MAX_SCORES = 3 

def getName(): 
    return raw_input("Please enter the name you wish to add: ").strip() 
def getScore(): 
    return raw_input("Please enter the high score: ").strip() 
def log(*msg): 
    print "\t[LOG] " + " ".join([word for word in msg]) 


if __name__ == "__main__": 

    # Get new name and Score: 
    newName = getName() 
    newScore = getScore() 
    log("NewUser and NewScore = %s,%s" % (newName,newScore)) 

    # open file and get actual scores: 
    log("Opening score file: %s" % SCORE_FILENAME) 
    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() 
    log("Actual score table: %s" % actualScoresTable) 

    # update scores or insert new record: 
    new = True 
    for index, record in enumerate(actualScoresTable): 
     if record["name"] == newName: 
      # The user exists in scores table, check how many scores he has: 
      if len(record["scores"]) >= MAX_SCORES: 
       # Max. Scores permitted. What to do here? 
       log("Actual user '%s' already have %s scores '%s'. What we have to do now?" % (newName, MAX_SCORES, ",".join(record["scores"]))) 
      else: 
       log("Add score '%s' to '%s' that now have [%s]" % (newScore,newName,",".join(record["scores"]))) 
       actualScoresTable[index]["scores"].append(newScore) 
      new = False 
      break 
    if new: 
     log("User '%s' not exists in actual Scores Table. So append it." % newName) 
     actualScoresTable.append({ 
      "name": newName, 
      "scores": [newScore], 
     }) 

    # Save result to file and close it: 
    scoresFile = open(SCORE_FILENAME, "w+") # Truncating file (write all again) 
    for record in actualScoresTable: 
     scoresFile.write("%s,%s\n" % (record["name"], ",".join(record["scores"]))) 
    log("Writing changes to file: %s" % actualScoresTable) 
    scoresFile.close() 

ПРИМЕЧАНИЕ:

  1. Есть много различных изменений, чтобы улучшить это решение (работать в открытом файле с with, прямой записью если это запись update -not truncate file, ...). Я хотел, чтобы этот путь стал более понятным из-за вашего прозвища: «learningpython»;)
  2. Я не знаю, что вы хотите сделать, если у пользователя уже есть три балла (сначала начать, удалить из первого добавленного, удалить из последнего добавил (...), так что я did't код, который ..

EDIT, чтобы соответствовать новым требованиям:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

# name,score_1,score_2,score_3 
# append score to a user 
# |_ if user not exists: add user and new score 
# |_ an user can't have more than 3 scores 


SCORE_FILENAME = "highscores.txt" 
MAX_SCORES = 3 

def getName(): 
    return raw_input("Please enter the name you wish to add: ").strip() 
def getScore(): 
    return raw_input("Please enter the high score: ").strip() 
def log(*msg): 
    print "\t[LOG] " + " ".join([word for word in msg]) 


if __name__ == "__main__": 

    # Get new name and Score: 
    newName = getName() 
    newScore = getScore() 
    log("NewUser and NewScore = %s,%s" % (newName,newScore)) 

    # open file and get actual scores: 
    log("Opening score file: %s" % SCORE_FILENAME) 
    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() 
    log("Actual score table: %s" % actualScoresTable) 

    # update scores or insert new record: 
    new = True 
    for index, record in enumerate(actualScoresTable): 
     if record["name"] == newName: 
      # The user exists in scores table, append new score: 
      log("User '%s' exists in actual Scores Table. So append score '%s' to him." % (newName,newScore)) 
      actualScoresTable[index]["scores"].append(newScore) 
      # if now we have more than 3 scores, we delete the first one (the oldest one): 
      if len(record["scores"]) > MAX_SCORES: 
       log("User '%s' reached max. scores record history, so append score '%s' to him, and delete the oldest: '%s'" % (newName,newScore,actualScoresTable[index]["scores"][0])) 
       actualScoresTable[index]["scores"].pop(0) # OR del actualScoresTable[index]["scores"][0] 
      new = False 
      break 
    if new: 
     log("User '%s' not exists in actual Scores Table. So append it." % newName) 
     actualScoresTable.append({ 
           "name": newName, 
           "scores": [newScore], 
           }) 

    # Save result to file and close it: 
    scoresFile = open(SCORE_FILENAME, "w+") # Truncating file (write all again) 
    for record in actualScoresTable: 
     scoresFile.write("%s,%s\n" % (record["name"], ",".join(record["scores"]))) 
    log("Writing changes to file: %s" % actualScoresTable) 
    scoresFile.close() 
+0

Отлично. В конечном счете, когда пользователь имеет шестьдесят, я хотел бы, чтобы программа была с терпеть снова, удаляя и заменяя первый балл. Любой дополнительный.help был бы оценен. – learningpython

+0

Чтобы снова запустить счет с пользователем в той же позиции файла, вам просто нужно вставить это 'if' (' if len (запись ["score"])> = MAX_SCORES') следующее: 'actualScoresTable [ index] ["score"] = [newScore] ' – aabilio

+0

Извините, я не очень хорошо себя объяснил. Мне нужно, чтобы программа продолжала переписывать баллы (замените затем один за другим), чтобы в любой момент список содержал последние 3 или последние 3 балла. Еще раз любая помощь будет оценена. – learningpython

Смежные вопросы