Python ValueError:解包的值太多(预期为 2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24150952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: too many values to unpack (expected 2)
提问by Ziggy
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.
在我正在使用的 Python 教程书中,我输入了一个用于同时分配的示例。运行程序时出现上述 ValueError,但不知道为什么。
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)
回答by Ziggy
Judging by the prompt message, you forgot to call str.split
at the end of the 8th line:
从提示信息来看,你忘记str.split
在第8行末尾调用:
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'
>>>
回答by shaktimaan
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
上面的代码在 Python 2.x 上可以正常工作。因为input
作为的行为raw_input
,随后由eval
有关Python 2.x的作为记录在这里- 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.
但是,上面的代码会引发您在 Python 3.x 上提到的错误。在 Python 3.x 上,您可以在用户输入上使用ast
模块的literal_eval()
方法。
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()
回答by DexJ
This is because behavior of input changed in python3
这是因为输入的行为在 python3 中发生了变化
In python2.7 input returns value and your program work fine in this version
在 python2.7 输入返回值,你的程序在这个版本中工作正常
But in python3 input returns string
但是在python3中输入返回字符串
try this and it will work fine!
试试这个,它会工作得很好!
score1, score2 = eval(input("Enter two scores separated by a comma: "))
回答by X.Na
that means your function return more value!
这意味着您的函数返回更多值!
ex:
前任:
in python2 the function cv2.findContours()
return --> contours, hierarchy
在python2中函数cv2.findContours()
返回-->contours, hierarchy
but in python3 findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
但是在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.
因此,当您使用这些函数时,contours, hierachy = cv2.findContours(...)
python2 很好,但是在 python3 函数中,将 3 个值返回到 2 个变量。
SO ValueError: too many values to unpack (expected 2)
SO ValueError:解包的值太多(预期为 2)