我在编写测验,学生完成测验后,保存分数时,我试图确保学生的最后三个分数被保存到他们的名字中。如果他们之前没有完成测验,则会在文本文件中为他们写一个新行。ValueError:需要多个值来解压
filename = (str(class_number) + 'txt')
with open(filename, 'a') as f:
f.write(str(name) + " : " + str(score) + '\n')
with open(filename) as f:
f.seek(0)
scores = f.readline()
from collections import deque
for line in scores:
name, score = line.rstrip('\n').split(',')
score= int(score)
if name not in filename:
filename[name] = deque(maxlen=3)
temp_q = filename[name]
temp_q.append(str(score))
filename[name] = temp_q
filehandle = open (filename, 'w')
for key,values in filename.iteritems():
filehandle.write(name + ',')
filehandle.write (','.join(list(values)) + '\n')
filehandle.close()
这是错误:
name, score = line.rstrip('\n').split(',')
Value error need more than 1 value to unpack
你有没有试过看'线'是什么? – jonrsharpe
[ValueError:需要多个值才能解包]可能的重复(https://stackoverflow.com/questions/19749077/valueerror-need-more-than-1-value-to-unpack) –