0
import datetime

def getDays(day = None):
    outer = []
    if day == None:
        day = datetime.date.today()
    if day.strftime('%A') == "Monday":
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        print(outer)
        return outer
    else:
        getDays(day = day + datetime.timedelta(days=1))

print(getDays())

In my second if statement, I return OUTER and print OUTER. Printing OUTER gives my desired output of ['August 10', 'August 11', 'August 12', 'August 13', 'August 14']

Returning OUTER returns None

Why can't I get it to return the same as what is printed?

  • Yes, I believe this provided me with the correct answer, thank you! – Hamza Sait Aug 08 '20 at 16:05
  • Does this answer your question? [Recursive function returning none in Python](https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python) – Carcigenicate Aug 08 '20 at 17:08

2 Answers2

0
def getDays(day = None, outer = []):

    if day == None:
        day = datetime.date.today()
    if day.strftime('%A') == "Monday":
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        day = day + datetime.timedelta(days=1)
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        return outer
    else:
        return getDays(day = day + datetime.timedelta(days=1), outer = outer)

I have figured out what was wrong, all I needed was to include return in the else statement and add the variables to the functions so when it recurses back it carries the values added. Thanks to Carcigenicate for providing useful resources.

  • I'm glad you found that post useful. Instead of posting an answer though, please just accept the suggested duplicate. It will help point anyone who comes across your question to the established existing resource. – Carcigenicate Aug 08 '20 at 16:09
0

The purpose of your code as I understand, is to print the 5 weekdays, starting from next Monday. When a recursive call returns back, it goes to the line right after the call. The image of printFun [here][1] might help.
In your case, outer will be [] at every iteration of the recursion except when you print it.
To fix your code, do this:
import datetime

def getDays(day = None, outer=[]):
    #outer = []         # If not commented then outer is empty when recusion comes back
    if day == None:
        day = datetime.date.today()
    if day.strftime('%A') == "Monday":
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        day = day + datetime.timedelta(days=1)
        outer.append(day.strftime(str(day.strftime("%B")) + " " + str(day.day)))
        #print(outer)
        #return outer
    else:
        getDays(day = day + datetime.timedelta(days=1))
        print(".")   # See how call comes back to this line
        return outer

print(getDays())
# or play with other dates
#print(getDays(datetime.datetime.fromisoformat('2020-08-04')))

ouput

.
.
['August 10', 'August 11', 'August 12', 'August 13', 'August 14']

Thanks

FEldin
  • 131
  • 7