Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive function Dates
#1
Hi There,

I am new to python and I am trying to create a recursive function which will return all the dates between 2017,2,28 and 2017,3,10.I have set a base case with the start date and the end date 2017,3,10.I am not too sure where I am going wrong.Your help would be much appreciated.

from datetime import date,timedelta,datetime
def daterec(start,end,timedelta):
  if start == date(2017,2,28):
    return start
    return daterec(start + timedelta(days=1)
for dat in daterec(date(2017, 2, 28), date(2017, 3, 10), timedelta(days=1)):
    print(dat)
Reply
#2
Please use Python tags around your code. I added it for you this time.

The return statement stops a function. So when daterec hits return start it stops processing and never gets to the second return statement. Also, you need another close-parentheses at the end of your second return statement.

Why the if condition? Your function will only work if start is 2/28/2017. If that's what you want, I would remove the start parameter and define start within the function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Why do you need a list of dates?
If your goal is to use them to find something between two dates, there are
much simpler ways to do this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 584 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,272 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,397 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,033 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,715 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,920 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Recursive function returns None, when True is expected akar 0 3,398 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial using recursive function spalisetty06 12 4,100 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  Recursive Function sridhar 7 2,837 Jul-14-2020, 07:53 PM
Last Post: deanhystad
  Nested Recursive Function not Returning Etotheitau 2 2,270 May-09-2020, 06:09 PM
Last Post: Etotheitau

Forum Jump:

User Panel Messages

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