"ValueError: too many values to unpack" when trying to read two numbers as input
Asked Answered
R

6

16

I was trying to run this code:

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 call main() I get this ValueError:

ValueError: too many values to unpack (expected 2)

What is wrong with this code?

Rotatory answered 30/9, 2013 at 15:48 Comment(0)
A
18
  1. You need to split the input you receive, because it arrives all in one string
  2. 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.
Axle answered 30/9, 2013 at 15:50 Comment(0)
A
12

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

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
>>>
Arlyn answered 30/9, 2013 at 15:50 Comment(0)
E
5

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.

The other answers have perfectly good suggestions for doing what you really wanted so I won't repeat them here.

Esperanzaespial answered 30/9, 2013 at 15:55 Comment(0)
C
0
>>>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
Cocoa answered 29/1, 2019 at 9:25 Comment(1)
Checkout the right answer, you will need to convert them as numbers afterwards. Furthermore, do not get what your answer is an addition to other answers?Pouched
C
-1

If you use args and give less values on running the file it will show you this error.

**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.
Cetinje answered 20/6, 2018 at 9:41 Comment(0)
C
-1

I had this issue when trying to access the tuple value.

You can use this to get the first value of the tuple returned from get_or_create:

user, _ = User.objects.get_or_create(
     ...
)
# email = user.email
# ...
Colicroot answered 30/7, 2024 at 15:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.