简体   繁体   中英

ValueError: too many values to unpack python

s = ('con', 'str', 'wit', 'int', 'dex', 'mp', 'p.def', 'm.def', 'p.atack', 'm.atack') 
c.execute("SELECT con, str, wit, _int, dex, mp, mdef, pdef, patack, matack FROM warrior_stat")
t = c.fetchone()[:]
for s1, t1 in s, t: print "%020s, " - ", %010s, '\n'" % (s, t) 

Why do I have this error:

Traceback (most recent call last):
  File "./test.py", line 49, in <module>
    for s1, t1 in s, t: print "%020s, " - ", %010s, '\n'" % (s, t)
ValueError: too many values to unpack

How can I fix it?

thanks for all comments !!! i am printing %(s, t) instead (s1, t1) and zip(s, t) worked correctly after this corection

dont make +1 into reputation. but my reputation is low

I think there should be single quotes inside

print "%020s, " - ", %010s, '\n'" % (s, t)

In this case only second part of string is formatted

", %010s, '\n'" % (s, t)

And here as You can see only one place holder for value but 2 value is passed So this is incorrect.

and I don't know why \\n is quoted. It seems that this line should be following:

print "%020s -  %010s, \n" % (s, t)

you need to zip two lists, and it should be in one string:

for s1, t1 in zip(s, t):
    print "%020s - %010s \n" % (s, t) 

To be strict, your 2nd problem would probable be worth opening another question, because now you have a problem elsewhere.

With

print "%020s, " - ", %010s, '\n'" % (s, t) 

you apply the % operation to ", %010s, '\\n'" , which is clearly not correct.

Even if it was, you'd get another error: you try to subtract the resulting string from "%020s, " which doesn't work as well.

Try

print "%020s - %010s" % (s, t)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM