Python Forum
valueError too many values to unpack
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
valueError too many values to unpack
#1
Can anyone tell me what I am doing wrong, I thought this was something you could do in python

a, b = input("Enter two numbers> ")
Reply
#2
You have 2 options:

1.
a, b = input("Enter two numbers> "), input("Enter two numbers> ")

2.
a = input("Enter two numbers> ")
a = a.split(' ')

# a becomes a list of 2 strings ['1', '2']
# You will have to convert those to integers.
Reply
#3
What version of Python are you using? What are you providing when input() asks for two numbers?

It looks like you're using Python 3, and I'll imagine an input like so
Output:
>>> a, b = input("> ") > 1 2 Traceback (most recent call last):  File "<stdin>", line 1, in <module> ValueError: too many values to unpack (expected 2)
Recall that input() gives you a string, so the way the code works is much like this
Output:
>>> a, b = "1 2" Traceback (most recent call last):  File "<stdin>", line 1, in <module> ValueError: too many values to unpack (expected 2)
Python isn't trying to interpret the contents of the string as integers here.
Output:
>>> a, b = "12" >>> a '1' >>> b '2'
What you could do is get the string from input(), use str.split (based on whether you want to use whitespace, commas, or whatever) and then use a comprehension on that split string to turn the contents into ints, or floats.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply
#4
(Apr-23-2017, 06:10 PM)idontreallywolf Wrote: a, b = input("Enter two numbers> "), input("Enter two numbers> ")
That behavior would cause the prompt to happen twice, so it would be like you were asking for four numbers.

(Apr-23-2017, 06:10 PM)idontreallywolf Wrote: a = input("Enter two numbers> ") a = a.split(' ') # a becomes a list of 2 integers ['1', '2']
As the list there shows, it's a list of strings, not ints. Another step is required to turn the strings into ints.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply
#5
(Apr-23-2017, 06:17 PM)micseydel Wrote:
(Apr-23-2017, 06:10 PM)idontreallywolf Wrote: a, b = input("Enter two numbers> "), input("Enter two numbers> ")
That behavior would cause the prompt to happen twice, so it would be like you were asking for four numbers.

(Apr-23-2017, 06:10 PM)idontreallywolf Wrote: a = input("Enter two numbers> ") a = a.split(' ') # a becomes a list of 2 integers ['1', '2']
As the list there shows, it's a list of strings, not ints. Another step is required to turn the strings into ints.

Indeed, I have forgotten to edit the text inside.

isn't it obvious that the last step requires conversion from str to int?
Reply
#6
Hello!
When you ask for an input what you get is a single string object. What is happening when Python unpack the numbers is to attempt to assign each symbol to the variables. If you type 1 2 as an input, the length of the string is 3 - '1', ' ', '2'. So you have three values to unpack to two variables. You have two options. To add split() at the end of the input(). To get the string and unpacking it after the input.

Here is what is happening:

In [1]: a, b = input("Enter two numbers> ")

Enter two numbers> 1 2 # three symbols
Error:
ValueError                                Traceback (most recent call last) <ipython-input-1-88647334f199> in <module>() ----> 1 a, b = input("Enter two numbers> ") ValueError: too many values to unpack (expected 2)
In [2]: a, b = input("Enter two numbers> ").split()
Enter two numbers> 1 2 # three symbols

In [3]: a, b = input("Enter two numbers> ")
Enter two numbers> 12 # two symbols

In [4]: print(a, b)
1 2
Still, the type of the a and b is string. You have to int() it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
Thanks all, I thought it would be simple. In this particular case, I just needed the variables to take a value, and to end up with two strings is good enough. Solution was as follows:
a_str, b_str = input("Enter two numbers> ").split()
a_int = int(a)
b_int = int(b)
Split returns strings, hence the conversion afterwards.
Reply
#8
What's with the hungarian notation?  Do you really need both the string and int?  Or can you just use the same variables?

Also, instead of doing the conversion for each variable on it's own line, you could just do both at once:
>>> a, b = map(int, input("> ").split())
> 42 3
>>> a
42
>>> b
3
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 421 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 1,965 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 1,723 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  struct.unpack failed Roro 2 800 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity
  Can't unpack values of dictionary with ** Snake 3 1,133 Mar-11-2020, 11:17 AM
Last Post: Snake
  Error: too many values to unpack Mike 1 2,928 Oct-30-2019, 03:07 PM
Last Post: buran
  How to unpack tuples in if statements saurkkum 1 1,993 May-05-2019, 09:43 AM
Last Post: Gribouillis
  too many values to unpack error fyec 1 6,150 Jun-28-2018, 01:48 AM
Last Post: ichabod801
  ValueError: not enough values to unpack (expected 2, got 1) Wolfpack2605 1 10,793 Jan-27-2018, 11:52 AM
Last Post: buran
  Dictionary Comprehension - ValueError: too many values to unpack (expected 2) samRddhi 2 6,206 Dec-22-2017, 09:25 AM
Last Post: samRddhi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020