How do I resolve this "Valueerror" in Python 3?

Asked

Viewed 48 times

-2

I’m having trouble solving this error:

Valueerror: Too Many values to unpack (expected 3)

This is my code:

def func(array, n):
    m = len(array)
    soma = 0
    saldo = []
    res = 0
    bool = False

    saldo = [0 for i in range(n+1)]


    for i in range(m):
        x, v, y = [x for x in range(n)] # **O erro está nessa linha**
        saldo[x] += v
        saldo[y] += v
        soma += v

    for i in range(1, n+1):
        if saldo[i] > 0:
            #res.append(saldo[i]) 
            res += saldo[i]
        elif saldo[i] < 0:
            #res.append(-saldo[i]) 
            res += -saldo[i]



    if res == soma:
        bool = False
    else:
        bool = True


    return bool, res
        
print(func([[3,1,10],[2,1,40],[2,4,30],[2,4,20]], 4))

Thank you in advance ;)

  • You have four elements in the list to unpack and only three variables to receive these elements. One element remains.

  • Perhaps it is better to explain what the code should do (what should be the result: given the values A and B, it should return X ey, etc.). Suddenly the solution doesn’t even need unpacking...

1 answer

0

Your 'n' is worth 4, and ,I did a test, when you put '...for x in range(n)' or '...for x in range(4)', the program understands that the beginning is 0, so it counts, 0, 1 ,2 ,3. which are 4 elements. To solve just put '...for x in range(1, n)'. I hope it helps. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.