python valueerror:解包的值太多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098478/
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
python valueerror : too many values to unpack
提问by DAVID
I am a python beginner . I was trying to run this code :
我是 python 初学者。我试图运行此代码:
def main():
print ( " This program computes the average of two exam scores . ")
score1,score2 = input ("Enter two scores separated by a comma:")
average = (score1 + score2)/2.0
print ("The average of the score is : " , average )
when I summon the main()
I got this ValueError
:
当我召唤main()
我得到这个ValueError
:
ValueError: too many values to unpack (expected 2)
what is wrong with this code ?
这段代码有什么问题?
采纳答案by wim
- You need to split the input you receive, because it arrives all in one string
- Then, you'll need to convert them to numbers, because the term
score1 + score2
will do string addition otherwise and you will get an error.
- 您需要拆分您收到的输入,因为它全部在一个字符串中到达
- 然后,您需要将它们转换为数字,因为该术语
score1 + score2
将进行字符串加法,否则您将收到错误消息。
回答by wim
You need to split on the comma:
您需要在逗号上拆分:
score1,score2 = input ("Enter two scores separated by a comma:").split(",")
Note however that score1
and score2
will still be strings. You will need to convert them into numbers using either float
or int
(depending on what number type you want).
但是请注意,score1
和score2
仍然是字符串。您需要使用float
或将它们转换为数字int
(取决于您想要的数字类型)。
See an example:
看一个例子:
>>> score1,score2 = input("Enter two scores separated by a comma:").split(",")
Enter two scores separated by a comma:1,2
>>> score1
'1'
>>> score1 = int(score1)
>>> score1
1
>>> score1 = float(score1)
>>> score1
1.0
>>>
回答by Mark Ransom
The input arrives as a single string. But Python has a split personality when it comes to strings: it can treat them as a single string value, or as a list of characters. When you tried to assign it to score1,score2
it decided you wanted a list of characters. Evidently you typed more than two characters, so it said you had too many.
输入作为单个字符串到达。但是 Python 在字符串方面有分裂的个性:它可以将它们视为单个字符串值,或者作为一个字符列表。当您尝试将其分配给它时,score1,score2
您决定需要一个字符列表。显然你输入了两个以上的字符,所以它说你有太多了。
The other answers have perfectly good suggestions for doing what you really wanted so I won't repeat them here.
其他答案对做你真正想做的事情有很好的建议,所以我不会在这里重复。
回答by Yog
If you use args and give less values on running the file it will show you this error.
如果您使用 args 并在运行文件时提供较少的值,它将显示此错误。
**To rectify that give correct values **
**纠正给出正确的值**
from sys import argv
one, two, three,four,five = argv
c=input("Enter the coffee you need?: ")
print("File Name is ",one)
print("First you need ",two)
print("The you need",three)
print("Last you need",four)
print("Last but not least you need",five)
print(f"you need above mentioned items to make {c}.")
While running code give it like this: **python hm5.py milk coffeepowder sugar water**
milk == two
coffeepowder ==three
sugar == four
water == five
one == file name (you don't need to give it while running
My output:
Enter the coffee you need?: filter coffee
First you need milk
The you need coffeepowder
Last you need sugar
Last but not least you need water
you need above mentioned items to make filter coffee.
回答by pushpika liyanaarachchi
>>>number1,number2 = input("enter numbers: ").split(",")
enter numbers: 1,2
>>> number1
'1'
>>> number2
'2'
Then you can convert into integers
然后你可以转换成整数
>>> number1 = int(number1)
>>> number2 = int(number2)
>>> average = (number1+number2)/2.0
>>> average
1.5