Python Forum
Issue with 'if' and datetime
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with 'if' and datetime
#1
I'd like my gui to have a different background based on the current hour and month. I've imported datetime. Its not returning any errors but returns the else statement.

second_now = datetime.datetime.now().second
minute_now = datetime.datetime.now().minute
hour_now = datetime.datetime.now().hour

day_now = datetime.datetime.now().day
month_now = datetime.datetime.now().month
year_now = datetime.datetime.now().year

if hour_now >= 7 <= 21 and month_now >= 5 <= 9:
    background = PhotoImage(file="C:\\Users\\blake\\PycharmProjects\\farming\\images\\farm_summer_day.png")
    background_label = Label(root, image=background)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
else:
    print("")
Reply
#2
As the month is currently 2, I would expect the conditional to fail and the else section to be run.

That said, your tests are written incorrectly. They're read from left to right, so 23 >= 7 <= 21 is true. If you want to see if the hour is between 7 and 21, you should write it as 7 <= hour_now <= 21.
Reply
#3
Thanks for catching that math error lol.

Quote:As the month is currently 2, I would expect the conditional to fail and the else section to be run.
I actually copied the wrong if statement.
Here's where I'm at with your correction

if 9 <= hour_now <= 18 and 9 < month_now <= 3:
    background = PhotoImage(file="C:\\Users\\blake\\PycharmProjects\\farming\\images\\farm_fall_day.png")
    background_label = Label(root, image=background)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
else:
    print("")
Still not working but I suspect we're closer to the solution now Undecided
Reply
#4
There is no value that month_now could be that is simultaneously bigger than 9 and less than 3. If you want it between 3 and 9 (including 3), then 3 <= month_now < 9 would work.
blakefindlay likes this post
Reply
#5
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.
Serafim likes this post
Reply
#6
Thanks for all the help. You're right deanhystad,
Quote:You need to learn how to debug your code.
I developed too much of a dependency on the forum. After seeing the solution I totally could've figured this one out myself if I looked at it long enough.

Thanks for the advice, just gonna keep on working at it.
Reply
#7
"looking at it" is not a useful debugging technique. You wrote the code in a way you thought was correct. When you "look at it" you are going to see it as correct. Debugging is really just testing with the purpose of fixing your code. Debugging is an active process. Instead of "looking" you should be "testing". If you have a debugger, single step through the code and verify it works as it should. If you don't have a debugger, or don't like using the debugger, add print statements to you code or extract snippets and test them in little test programs.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Datetime format issue with z sks3286 2 7,399 Apr-07-2020, 12:26 PM
Last Post: sks3286
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,600 Sep-18-2019, 08:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

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