I am learning python and going through some interactive exercises. Specifically, I'm working on Friday the 13th.
I have rewritten several iterations of this but can never seem to lock it down. With this version, it seems to get hung up when run with the simulated start date of 2025-06-12 which means there's a problem with the "this month" section. Since it returns an accurate Friday the 13th except not 2025-06-13, I suspect it's a problem with the elif
statement, particularly the
and date.fromisoformat(current_year '-' current_month '-13').weekday == 4:
Here's the most recent iteration of this.
def friday_the_13th():
from datetime import date
current_year = str(date.today().year)
current_month = str(date.today().month)
if len(current_month) == 1: current_month = '0' current_month
#Function to increment to the 13th of next month
def NextMonth13(startdate):
lst_date = str(startdate)
lst_date = lst_date.split('-')
month = int(lst_date[1])
if month == 12:
year = str(int(lst_date[0]) 1)
month = '01'
return str(year '-' month '-' '13')
else:
year = lst_date[0]
month = str(month 1)
if len(month) == 1: month = '0' month
return str(year '-' month '-' '13')
# Return today if today is Friday the 13th
if date.today().weekday() == 4 and date.today().day == 13:
return date.today()
# Check if this month's 13th is in the future and if it's a Friday
elif date.today().day < 13 and date.fromisoformat(current_year '-' current_month '-13').weekday == 4:
return str(date.fromisoformat(current_year '-' current_month '-13'))
#Check next month and return result if Friday 13
else:
result = NextMonth13(date.today())
while not (date.fromisoformat(result).weekday() == 4):
result = NextMonth13(result)
if date.fromisoformat(result).weekday() == 4:
return result
Would someone mind giving me some guidance on what I might be doing wrong?
CodePudding user response:
First, your error is that you forgot the parenthesis after the weekday
method call: date.fromisoformat(current_year '-' current_month '-13').weekday() == 4
(FYI, date.fromisoformat(current_year '-' current_month '-13').weekday
returns the memory address of the method, something like this <built-in method weekday of datetime.date object at 0x7fa4e36058f0>
. As you can see, it is nowhere near the result you were expecting, so it was normal for your program to behave this way.)
Second, you are needlessly complicating yourself by doing str
conversions all the time:
def friday_the_13th():
from datetime import datetime, timedelta
days = 0
today = datetime.today()
while True:
curr = today timedelta(days=days)
if curr.day == 13 and datetime.weekday(curr) == 4:
return str(datetime.date(curr))
days = 1
This is more readable and less prone to error as you only convert to string at the end, after you've handled all your calculations.
CodePudding user response:
Not sure if it helps but to calculate the future Friday 13th you can do something like:
import datetime
def get_13th_future(startdate, months):
result=[]
year=startdate.year
month=startdate.month
checkdate = datetime.date(year=year, month=month, day=13)
for i in range(month):
if checkdate.weekday()==4:
result.append(checkdate.isoformat())
month =1
if month==13:
year =1
month=1
checkdate=datetime.date(year=year,month=month,day=13)
return result
startdate=datetime.datetime.now()
print(get_13th_future(startdate,1000))
If you like to search for a specific date you might construct a set instead of the list.