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


Messages In This Thread
valueError too many values to unpack - by iFunKtion - Apr-23-2017, 05:51 PM
RE: valueError too many values to unpack - by wavic - Apr-23-2017, 10:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 534 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  ValueError - Formatting issue when values enter thousands phillyfa 4 1,248 Apr-20-2023, 06:22 PM
Last Post: phillyfa
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,278 May-17-2022, 11:38 AM
Last Post: Larz60+
  unpack dict menator01 1 1,244 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,399 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 2,916 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 9,003 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 6,108 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  struct.unpack failed Roro 2 3,403 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity
  Can't unpack values of dictionary with ** Snake 3 3,635 Mar-11-2020, 11:17 AM
Last Post: Snake

Forum Jump:

User Panel Messages

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