我试着将3个最近的分数保存到一个列表中,然后将该列表附加到一个主类列表中,创建一个列表,每个学生和他们的分数作为一个单独的列表。该代码设法将分数附加到现有列表,但不显示第一个分数并在添加3分之前删除它。有人能帮助我吗?存储最后3个分数?
Student = [] #new list created each time for student
max_scores = 3 #3 scores only at a time in the student's list
if ClassName == "10x1": #if student's input for class is 10x1
for userName in Class10x1:
if userName in Student and len(str(score)) >= max_scores:
Student.pop([1]) #removes second element in list
Student.append(score) #adds most recent score
break #stops code
elif userName in Student and len(str(score)) < max_scores:
Student.append(score)
break
else:
Student.append(userName)
Student.append(score)
break
Class10x1.append(Student) #student's list is appended to main class list
上面的代码是打印:
[[]]
[['Jagraj', 1], ['Jagraj', 1]]
[['Jagraj', 1, 3], ['Jagraj', 1, 3], ['Jagraj', 1, 3]]
但是我想打印这样的:
[['Jagraj', 0, 1, 3]]
,然后一旦学生有一个4字试验,率先得分被删除,因此它变成[['Jagraj',1,3,5]]有没有办法做到这一点,或者我必须重建我存储数据的方式?
得分是什么样子? len(str(分数))对我来说非常令人不安 – Dionys
它只是一个测试完成后产生的整数 –
'len(str(分数))'返回分数位数。我不认为这就是你想要测试的。也许'len(学生)+ 1> = max_scores'? – Dionys