1

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)

3 Answers 3

3

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)
Sign up to request clarification or add additional context in comments.

5 Comments

ah, thank you very much for explaining the string thing, I did not know that. Unfortunately, it does need to be a list of tuples, because of the nature of the rest of the code, do you know of a way I could get this, and have the values as they currently are. EG: converting each one to a different type?.
also, have added the context of the code to the main Q
Glad to be of help. Now I see the context around your posted code, it reaffirms my belief that you don't need currenttime at all. Please give the solution I provided using zip a try. It replaces your while loop, so you don't need an inner for loop at all.
don't suppose you have any experience with matplotlib and tkinter? I want it to be making 2 circle diagrams, but from this, it is only making one
Sorry but unfortunately I don't. You would get much better answers if you post it as a new question so people with the right knowledge would be more able to help.
0

The following lines:

for percent, fail, title in currenttime:

implies that currenttime is a list of tuples, when it's a list in your example, and which translates to:

for (percent, fail, title) in currenttime:

what you should be doing instead if you wanna get the 3 elements of currenttime:

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

or make currenttime a tuple:

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

Comments

0

The source of a for command must be an iterable. Yours is an iterable which returns one string at each iteration. The first element returns "67", which has only two elements to unpack.

For the functionality you want, each element of currentime would have to be a triple. For instance:

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

In this case, each iteration yields three values to unpack.

1 Comment

ah, I see, thaankyou

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.