Python Forum
too many values to unpack error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
too many values to unpack error
#1
I am trying to solve 'too many values to unpack' error.
This code tries to print month name according to leap years and numbers of ends of the months.
How can I solve this error?
import calendar
year=2000
a=calendar.isleap(year)
if a ==False :
    print('this is not a leap year')
    x=[0,31,59,90,120,151,181,212,243,273,304,334]
    y=[31,59,90,120,151,181,212,243,273,304,334,365]
                    
    for i,j in zip(x, y):
                        if (i,j) == (0,31):
                            (i,j)=str('Jan')
                        if (i,j) == (31,59):
                            (i,j)=str('Feb')
                        if (i,j)==(59,90):
                            (i,j)=str('March')
                        if (i,j)==(90,120):
                            (i,j)=str('Apr')
                        if (i,j)==(120,151):
                            (i,j)=str('May')
                        if (i,j)==(151,181):
                            (i,j)=str('Jun')
                        if (i,j)==(181,212):
                            (i,j)=str('Jul')
                        if (i,j)==(212,243):
                            (i,j)=('Aug')
                        if (i,j)==(243,273):
                            (i,j)=str('Sep')
                        if (i,j)==(273,304):
                            (i,j)=str('Oct')
                        if (i,j)==(304,334):
                            (i,j)=str('Nov')
                        if (i,j)==(334,365):
                            (i,j)=str('Dec')
                            
                            print('Mont_'+ (x,y))
                            
else: 
    print ('This is a leap year')
    x=[0,31,60,91,121,152,182,213,244,274,305,335]
    y=[31,60,91,121,152,182,213,244,274,305,335,366]
    for i,j in zip(x, y):
                        if (i,j) == (0,31):
                            (i,j)=str('Jan')
                        if (i,j) == (31,60):
                            (i,j)=str('Feb')
                        if (i,j)==(60,91):
                            (i,j)=str('March')
                        if (i,j)==(91,121):
                            (i,j)=str('Apr')
                        if (i,j)==(121,152):
                            (i,j)=str('May')
                        if (i,j)==(152,182):
                            (i,j)=str('Jun')
                        if (i,j)==(182,213):
                            (i,j)=str('Jul')
                        if (i,j)==(213,244):
                            (i,j)=('Aug')
                        if (i,j)==(244,274):
                            (i,j)=str('Sep')
                        if (i,j)==(274,305):
                            (i,j)=str('Oct')
                        if (i,j)==(305,335):
                            (i,j)=str('Nov')
                        if (i,j)==(335,366):
                            (i,j)=str('Dec')
                            
                            print('Mont_'+ (x,y))
Reply
#2
This is your problem:

(i, j) = str('Jan')
You have two variables on the left side, and an iterable on the right ('Jan'). So python goes through the iterable and assigns 'J' to i, and 'a' to j, and then raises the error when it has nowhere to assign 'n'. If you want to assign 'Jan' to both i and j, you need:

i, j = 'Jan', 'Jan'
Note that I didn't use str('Jan'). That's because str('Jan') is redundant: 'Jan' is already a str.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 411 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 1,625 May-11-2022, 07:12 AM
Last Post: normanwolf
  unpack dict menator01 1 1,159 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,280 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 2,836 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 8,819 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  Argparse error when inputting values tqader 2 2,822 Sep-11-2020, 07:42 PM
Last Post: buran
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 5,881 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  struct.unpack failed Roro 2 3,290 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity
  Can't unpack values of dictionary with ** Snake 3 3,503 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