1π
I think the problem is with this condition.
while lastmonth <= int(month) or lastyear<int(year):
During December, month=12
, so lastmonth <= int(month)
will always be True
. So the loop whill always return True, even once lastyear
is more that the current year
.
You want to loop if the loop is in the previous year, or if the loop is in the current year and the month is not in the future. Therefore, I think you want to change it to the following:
while lastyear < year or (lastyear == year and lastmonth <= month):
To be sure that the code is working and to understand it, you need to add lots of print statements to the loops, see how lastmonth
and lastyear
change, and check that the loop exits when you expect it to. You also need to test it for other values of year
and month
so that it doesnβt break next month. Ideally you want to extract this bit of the code into a separate function. It would be easier to understand the loop if it only returned a list of (month, year)
integers, instead of doing lots of date formatting at the same time. Then it would be easier to add unit tests.