Python Forum
too many values to unpack error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: too many values to unpack error (/thread-11204.html)



too many values to unpack error - fyec - Jun-27-2018

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



RE: too many values to unpack error - ichabod801 - Jun-28-2018

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.