【问题标题】:ValueError: too many values to unpack (expected 2)ValueError:要解包的值太多(预期为 2)
【发布时间】:2014-07-31 19:17:43
【问题描述】:

在我正在使用的 Python 教程书中,我键入了一个为 同时分配 给出的示例。运行程序时出现上述ValueError,不知道是什么原因。

代码如下:

#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()

这是输出。

>>> 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)

【问题讨论】:

标签: python input iterable-unpacking


【解决方案1】:

从提示信息来看,你忘记在第8行末尾调用str.split

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

这样做会拆分逗号上的输入。请看下面的演示:

>>> 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】:

    上面的代码可以在 Python 2.x 上正常工作。因为input 在 Python 2.x 上的行为类似于 raw_input,后跟 eval,如此处所述 - https://docs.python.org/2/library/functions.html#input

    但是,上面的代码会引发您在 Python 3.x 上提到的错误。在 Python 3.x 上,您可以在用户输入上使用 ast 模块的 literal_eval() 方法。

    这就是我的意思:

    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()
    

    【讨论】:

    • 呵呵,从来没有考虑过使用literal_eval
    【解决方案3】:

    这是因为在 python3 中输入的行为发生了变化

    在 python2.7 中输入返回值,你的程序在这个版本中工作正常

    但在 python3 中输入返回字符串

    试试这个,它会正常工作的!

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

    【讨论】:

      【解决方案4】:

      这意味着您的函数返回更多价值!

      例如:

      在python2中函数cv2.findContours()返回 --> contours, hierarchy

      但是在python3中findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -&gt; image, contours, hierarchy

      所以当你使用这些函数时,contours, hierachy = cv2.findContours(...) 在 python2 中很好,但在 python3 函数中返回 3 值到 2 变量。

      SO ValueError: 要解压的值太多(预期为 2)

      【讨论】:

        猜你喜欢
        • 2017-08-29
        • 1970-01-01
        • 2016-05-24
        • 1970-01-01
        • 2017-12-14
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多