不应在for循环中出现值错误?

时间:2019-04-12 22:19:01

标签: python for-loop valueerror

这是我的代码:

    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)

3 个答案:

答案 0 :(得分:2)

声明:

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)

答案 1 :(得分:0)

以下几行:

for percent, fail, title in currenttime:

在您的示例中为currenttime时,它是一个元组列表,并且表示为:

for (percent, fail, title) in currenttime:

如果您想获取currenttime的3个元素,应该怎么做:

percent = currenttime[0] 
fail = currenttime[1] 
title = currenttime[2]

或将currenttime设为元组:

currenttime = (perc[counter], fails[counter], titles[counter])
percent, fail, title = currenttime

答案 2 :(得分:0)

for命令的源必须是可迭代的。您的代码是可迭代的,每次迭代都返回一个字符串。第一个元素返回"67",只有两个要解压缩的元素。

要获得所需的功能,currentime each 元素必须为三元组。例如:

currenttime = [
    ['67', '1', 'subsection'],
    ['18', '5', 'main branch'],
    ...
]

在这种情况下,每次迭代都会产生三个要解包的值。