ValueError: too many values to unpack (expected 2)

懵懂的女人 提交于 2019-12-10 04:22:52

问题


In the Python tutorial book I'm using, I typed an example given for simultaneous assignment. I get the aforementioned ValueError when I run the program, and can't figure out why.

Here's the code:

#avg2.py
#A simple program to average two exam scores
#Illustrates use of multiple input

def main():
    print("This program computes the average of two exam scores.")

    score1, score2 = input("Enter two scores separated by a comma: ")
    average = (int(score1) + int(score2)) / 2.0

    print("The average of the scores is:", average)

main()

Here's the output.

>>> import avg2
This program computes the average of two exam scores.
Enter two scores separated by a comma: 69, 87
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    import avg2
  File "C:\Python34\avg2.py", line 13, in <module>
    main()
  File "C:\Python34\avg2.py", line 8, in main
    score1, score2 = input("Enter two scores separated by a comma: ")
ValueError: too many values to unpack (expected 2)

回答1:


Judging by the prompt message, you forgot to call str.split at the end of the 8th line:

score1, score2 = input("Enter two scores separated by a comma: ").split(",")
#                                                                ^^^^^^^^^^^

Doing so splits the input on the comma. See a demonstration below:

>>> input("Enter two scores separated by a comma: ").split(",")
Enter two scores separated by a comma: 10,20
['10', '20']
>>> score1, score2 = input("Enter two scores separated by a comma: ").split(",")
Enter two scores separated by a comma: 10,20
>>> score1
'10'
>>> score2
'20'
>>>



回答2:


The above code will work fine on Python 2.x. Because input behaves as raw_input followed by a eval on Python 2.x as documented here - https://docs.python.org/2/library/functions.html#input

However, above code throws the error you mentioned on Python 3.x. On Python 3.x you can use the ast module's literal_eval() method on the user input.

This is what I mean:

import ast

def main():
    print("This program computes the average of two exam scores.")

    score1, score2 = ast.literal_eval(input("Enter two scores separated by a comma: "))
    average = (int(score1) + int(score2)) / 2.0

    print("The average of the scores is:", average)

main()



回答3:


This is because behavior of input changed in python3

In python2.7 input returns value and your program work fine in this version

But in python3 input returns string

try this and it will work fine!

score1, score2 = eval(input("Enter two scores separated by a comma: "))



回答4:


that means your function return more value!

ex:

in python2 the function cv2.findContours() return --> contours, hierarchy

but in python3 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

so when you use those function, contours, hierachy = cv2.findContours(...) is well by python2, but in python3 function return 3 value to 2 variable.

SO ValueError: too many values to unpack (expected 2)



来源:https://stackoverflow.com/questions/24150952/valueerror-too-many-values-to-unpack-expected-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!