Using an if statement involving time in python

Dalton Johnson

So I've been trying to get an if statement to run if it is a certain time, and been trying this:

import datetime
try:
    while True:
        if(datetime.datetime.now().time() == (6, 20, 0, 0)):
            print("It's 6:20!")
except :
    pass

But nothing happens at 6:20.. why and how do I fix this?

chepner

The function you are calling doesn't return a tuple; it returns a datetime.time instance. Also, the odds of you calling the function at exactly 6:20 are vanishingly small.

success = False
while True:
    now = datetime.datetime.now().time()
    if now.hour == 6 and now.minute == 20:
        success = True
        print("It's 6:20!")

More than likely, you simply want to break out of the loop once success becomes true, meaning your code would look more like

while True:
    now = ...
    if now.hour == 6 ...:
        print("It's 6:20!")
        break

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using the current time in an IF statement

From Dev

MySQL select statement involving join

From Dev

problems with switch statement involving functions

From Dev

Switch statement involving string and char

From Dev

Using Python Queue with a "with" statement

From Dev

Tips on using the with statement in Python

From Dev

Using a conditional 'with' statement in Python

From Dev

Python regex: using or statement

From Dev

Using Python Queue with a "with" statement

From Dev

Python if statement using Modulo

From Dev

Selenium and Python using If statement

From Dev

Using case statement and if-else at the same time?

From Dev

finding a time range using case statement

From Dev

Meaning of a C statement involving char arrays

From Dev

Error involving subquery in SQL UPDATE statement

From Dev

Execution time using time.time() in Python

From Dev

Parsing JSON using Python and if statement

From Dev

Python if statement using CSV Data

From Dev

Infinite loop in python using for statement

From Dev

Using the with statement in Python 2.5: SyntaxError?

From Dev

Python error when using "not in" in a if statement?

From Dev

Using the with statement in Python 2.5: SyntaxError?

From Dev

Python error when using "not in" in a if statement?

From Dev

Using output of a python expression in an if statement

From Dev

python regular expression involving backslash

From Dev

Stuck on a Gaps and Islands query involving time periods

From Dev

Stuck on a Gaps and Islands query involving time periods

From Dev

Changing axis on scatterplot to fixed intervals involving time

From Dev

How to get last modified time of file while using infile statement?

Related Related

HotTag

Archive