我在編寫測驗,學生完成測驗後,保存分數時,我試圖確保學生的最後三個分數被保存到他們的名字中。如果他們之前沒有完成測驗,則會在文本文件中爲他們寫一個新行。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) –