Search code examples
pythonfor-loopvalueerror

Value error when there shouldn't be in for loop?


this is my code:

    while counter <= len(titles):
        currenttime = [perc[counter], fails[counter], titles[counter]]
        print(currenttime)
        for percent, fail, title in currenttime:

When I run it, I get a value error showing

ValueError: not enough values to unpack (expected 3, got 2)

But when I print current time, I get

['67', '1', 'subsection']

To me, this looks like 3 values, but obviously I am wrong, could someone enlighten me? I've had a look around, but have found no good answers shofar. any help would be greatly appreciated. Thanks

code context:

      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)

Solution

  • The statement:

    for percent, fail, title in currenttime:
    

    means to unpack each item in the currenttime list as a sequence, and yet each item in the currenttime list is just a string, which unpacks into characters, of which the first item has just two, resulting in the "not enough values to unpack (expected 3, got 2)" error.

    For your purpose, you should simply zip the 3 lists and iterate over the zip generator instead of a while loop with a counter and an inner for loop:

    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)