current_class = Classes[student_class] #Used to get the value of the key in Classes
class_score = open(current_class, 'a+') #Scores must be appended and not written to avoid overwriting data
class_format = (name + ' scored ' + str(Score) + '\n') #This is the format in which the text file will save the data as
class_score.write(class_format) #Writing the scores to the text file using the format above
class_read = class_score.read() #For some reason, the scores will not be added to the text file unless I add this text (Using OSX)
Я использую этот код для сохранения оценок (вводимое имя) в текстовый файл. Есть 3 текстовых файла, и в зависимости от того, в каком классе вы находитесь (класс ввода пользователя), он сохраняет оценку в классе 1, 2 или 3.
Однако этот процесс можно повторить 1000 раз, и если введено одно и то же имя, «Имя» может иметь 1000 баллов. Как бы я мог ограничить его, чтобы при добавлении оценки в текстовый файл он сохранял только 3 самых последних оценки
РЕДАКТИРОВАТЬ: я немного разработал код, но до сих пор не знаю, как заставить его заменить «самую старую» оценку.
current_class = Classes[student_class] #Used to get the value of the key in Classes
class_score = open(current_class, 'a+') #Scores must be appended and not written to avoid overwriting data
class_format = (name + ' scored ' + str(Score) + '\n') #This is the format in which the text file will save the data as
class_read = class_score.read()
class_count = class_read.count(name)
while True:
if class_count > 3:
else:
class_score.write(class_format) #Writing the scores to the text file using the format above
break
0
для аргументаbuffering
open, чтобы каждый раз сбрасывать запись. - person David Cullen   schedule 28.04.2016