这是我的代码:
while counter <= len(titles):
currenttime = [perc[counter], fails[counter], titles[counter]]
print(currenttime)
for percent, fail, title in currenttime:
当我运行它时,出现一个值错误,显示
ValueError: not enough values to unpack (expected 3, got 2)
但是当我打印当前时间时,我得到
['67', '1', 'subsection']
对我来说,这看起来像3个值,但显然我错了,有人可以启发我吗?我环顾四周,但没有发现良好的答案。任何帮助将不胜感激。谢谢
代码上下文:
n = 0
perc = list()
while n < len(piedata):
perc.append(piedata[n+2])
n += 3
print (perc)
n = 0
fails = list()
while n < len(piedata):
fails.append(piedata[n+1])
n += 3
print(fails)
n = 0
titles = list()
while n < len(piedata):
titles.append(piedata[n])
n += 3
print(titles)
counter = 0
while counter <= len(titles):
currenttime = [perc[counter], fails[counter], titles[counter]]
print(currenttime)
for percent, fail, title in currenttime:
piedata = [percent, (100-percent)]
fig = matplotlib.figure.Figure(figsize=(5, 5))
ax = fig.add_subplot(111)
ax.pie(piedata) # this is the information that the circle diagram will be made out of
ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])
circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
ax.add_artist(circle)
# this is the code for actually putting the circle diagram/pie chart on the screen
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.get_tk_widget().pack()
canvas.draw()
Label(window, text=(title, title), bg='light blue').pack()
counter += 1
window.mainloop()
print(percent)
print(fail)
该声明:
for percent, fail, title in currenttime:
表示要按currenttime
顺序解压缩列表中的每个项目,但列表中的每个项目currenttime
只是一个字符串,将其解压缩为字符,其中第一项只有两个,导致“没有足够的值要解压缩(预期3 ,得到2)“错误。
为了您的目的,您应该简单地压缩3个列表并遍历zip
生成器,而不是while
使用带有计数器和内部for
循环的循环:
for percent, fail, title in zip(perc, fails, titles):
piedata = [percent, (100 - percent)]
fig = matplotlib.figure.Figure(figsize=(5, 5))
ax = fig.add_subplot(111)
ax.pie(piedata) # this is the information that the circle diagram will be made out of
ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])
circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
ax.add_artist(circle)
# this is the code for actually putting the circle diagram/pie chart on the screen
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.get_tk_widget().pack()
canvas.draw()
Label(window, text=(title, title), bg='light blue').pack()
window.mainloop()
print(percent)
print(fail)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句