You need to learn how to debug your code. I am tired of seeing your name on posts that I know you can answer yourself.
The first thing you should have done is verify that you understand how datetime works. This is easily done by printing out the minute, hour, day, month and year. This would be my first step debugging the problem.
import datetime
now = datetime.datetime.now()
second_now = now.second
minute_now = now.minute
hour_now = now.hour
day_now = now.day
month_now = now.month
year_now = now.year
print(year_now, month_now, day_now, hour_now, minute_now, second_now)
Output:
2021 2 7 18 40 58
That all looks good.
The next thing I would do is deconstruct the if statement and see where I messed up.
hour_now = 12
month_now = 7
print(9 <= hour_now <= 18 and 9 < month_now <= 3)
Output:
False
Hmmm. I was thinking that would be True. I am going to break the condition into two pieces.
hour_now = 12
month_now = 7
print(9 <= hour_now <= 18)
print(9 < month_now <= 3)
Output:
True
False
The hour comparison is True, but the month comparison is False. True and False is False. There is a problem with the month comparison.
Looking at the month comparison I see it will be true if month > 9 and month >= 3. Is that correct? Probably not, because that is impossible. Is this the correct comparison?
hour_now = 12
month_now = 7
print(9 <= hour_now <= 18)
print(3 <= month_now < 9)
Output:
True
True
Next I try some different values for hour and month to verify that the logic is correct for all conditons. To help with test coverage I make a table.
Test Hour Month
---- ---- -----
hour < 9 and month < 3 2 2
hour < 9 and month = 3 2 3
hour < 9 and month > 3 < 9 2 6
hour < 9 and month = 9 2 9
hour < 9 and month > 9 2 11
hour = 9 and month < 3 9 2
hour = 9 and month = 3 9 3
hour = 9 and month > 3 < 9 9 6
hour = 9 and month = 9 9 9
hour = 9 and month ? 9 9 11
hour > 9 < 18 and month < 3 12 2
hour > 9 < 18 and month = 3 12 3
hour > 9 < 18 and month > 3 < 9 12 6
hour > 9 < 18 and month = 9 12 9
hour > 9 < 18 and month ? 9 12 11
hour = 18 and month < 3 18 2
hour = 18 and month = 3 18 3
hour = 18 and month > 3 < 9 18 6
hour = 18 and month = 9 18 9
hour = 18 and month ? 9 18 11
hour > 18 and month < 3 20 2
hour > 18 and month = 3 20 3
hour > 18 and month > 3 < 9 20 6
hour > 18 and month = 9 20 9
hour > 18 and month ? 9 20 11
Make combinations of hour and month for each test. Run the test and verify the test results are always correct.
Running tests like this will develop your debugging skills. Soon you will start seeing mistakes early in your debugging and you can uses fewer tests. Then the debug skill will start seeping over into your coding and you will see the errors as you type the code. Eventually you will see potential errors as you are designing your code.
It all starts by you stopping depending on others and answering your questions yourself.