0

I wrote three if statements. The first one I made it True so that I can test it. Isn't it supposed to stop at the first statement evaluated to true ? It is showing me that the first statement and the last else statement are true. I'm trying to just make the first if statement run as a test.

skipdays = {'Monday': '2019-08-19', 'Tuesday': {'2019-08-20', '2019-10- 
30'}}
skipday_date = date.today() - timedelta(days=3)
skipday_date = skipday_date.strftime("%Y-%m-%d")
skipday_day = date.today() - timedelta(days=3)
skipday_day = skipday_day.strftime('%A')


# Get all Mondays in the year
def allmondays(year):
   d = date(year, 1, 1)                    # January 1st
   d += timedelta(days=7 - d.weekday())  # First Monday
   while d.year == year:
      yield d
      d += timedelta(days=7)

# Get current year
current_year = int(date.today().strftime('%Y'))
# If today is Monday's cycle day then subtract 3 to get Friday's cycle day
for (skipday, skipdate),monday in zip(skipdays.items(), 
allmondays(current_year)):
    if skipday_day == skipday and skipday_date in skipdate:
        today = date.today() - timedelta(days=4)
        col = today.strftime('%b').lower()
        current_cycle_day = df[df[col] == today.day].index.values[0]
        print("Today is a skip day")
        print(current_cycle_day)
    elif date.today() == monday:
        today = date.today() - timedelta(days=3)
        col = today.strftime('%b').lower()
        current_cycle_day = df[df[col] == today.day].index.values[0]
        print("Today is a Monday")
# If not Monday then just subtract 1 to get yesterday's cycle day.
    else:
        today = date.today() - timedelta(days=1)
        col = today.strftime('%b').lower()
        current_cycle_day = df[df[col] == today.day].index.values[0]
        print("Today is a regular day")
        print(current_cycle_day)

current results:

 Today is a regular day
 16
 Today is a skip day
 13

expected results:

 Today is a regular day
 16
5
  • You're saying both "Today is a skip day" and "Today is a regular day" are printing? You must be running it twice then. Commented Aug 23, 2019 at 12:43
  • There's nothing about if/elif/else that would cause this. Must be a problem with your for clause, which does look a bit complicated at first glance. Commented Aug 23, 2019 at 12:45
  • If you're looping multiple times, one of the iterations is causing one to print, the other is causing something else to print. Do you actually want a loop here? The bodies of if and else will never both run. Commented Aug 23, 2019 at 12:46
  • Print your skipdays before the first if to understand which days are being sent through the loop and you should find the issue causing this Commented Aug 23, 2019 at 12:51
  • @Carcigenicate I need to loop over skipdays.items() for the first if statement. I also need to loop over the all_mondays(current_year) for the second if statement. If none of these are true i would need the else statement to run. This is so i can get the current_cycle_day for the corresponding If statement. I used zip because its the only way it would let me unpack the .items() without causing an unpacking error. Not sure if thats the right way. Commented Aug 23, 2019 at 13:17

1 Answer 1

1

Your for loop will iterate only two times because you zip list with all mondays with list that contains only two elements (that means as well that you will check only two first mondays of this year as well).

This for complicated loop is not necessary. Better would be to iterate once over skipdays.

today = date.today() - timedelta(days=1)
msg = "Today is a regular day"

for skipday, skipdate in skipdays.items():
    if skipday_day == skipday and skipday_date in skipdate:
        today = date.today() - timedelta(days=4)
        msg = "Today is a skip day"
        # no need to continue iteration
        break

# if msg == "Today is a regular day" that means that for sure it's not skip day
if date.today() in list(allmondays(current_year)) \
        and msg == "Today is a regular day":
    today = date.today() - timedelta(days=3)
    msg = "Today is a Monday"

col = today.strftime('%b').lower()
current_cycle_day = df[df[col] == today.day].index.values[0]

print(msg)
print(current_cycle_day)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.