1

I'm practising Python and I want to write a program that checks current time and see if it matches 2:12 pm and says: its lunch time So first I figured out using import time module, but I don't know how?

My problem is I don't know how to use time module for that. Or I'm using right syntax or not?

My code:

import time

bake=time()
if bake == '2:12':
    print ('its time to eating lunch')
else :
   print ('its not the time for eating lunch')
Biffen
  • 6,249
  • 6
  • 28
  • 36
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • bake = time() gives you the time since you started the program, not the current time – The Dude Aug 08 '16 at 09:41
  • 2
    It's the datetime module that you need. – polku Aug 08 '16 at 09:44
  • @TheDude no it doesn't, it gives you the number of seconds since the epoch. – Daniel Roseman Aug 08 '16 at 09:44
  • 1
    And who has lunch at 2.12? That's the middle of the afternoon :) – Daniel Roseman Aug 08 '16 at 09:45
  • dude its example :| so how can i give my current time? – Babak Abadkheir Aug 08 '16 at 09:47
  • 1
    Your current code would generate an error. You can look in the documentation on the `time` module for how to get the time. Next, it would never match the string you gave, because it doesn't produce that naturally. I recommend doing research. – TigerhawkT3 Aug 08 '16 at 09:52
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Aug 08 '16 at 09:52
  • 1
    In most cases, you can find your question to be answered already if you take time to properly identify your question. In this specific case, your question is not "checking time for executing if statement" but rather: "How to get current time in python and break up into hour,minute?" such as in the link i posted in my answer – Nikolas Rieble Aug 08 '16 at 09:54
  • like isaid im new in programming .i asked a question here to how to get my answer from expert guys .the meaning of this site is that to help each other.so like i said i tried run my code with my pretty simple 1 mounth skill and it didnt work.if u have problem with answering my question dont do that – Babak Abadkheir Aug 08 '16 at 09:59
  • 1
    Nobody has a problem with answering your question, we rather want to help you finding your information as fast as possible for further questions that you might have. – Nikolas Rieble Aug 08 '16 at 10:13
  • Im talking with that guy tigerhawkT3.and its my second time posting to stack i dont know the rules much .but i glad some one like u can help me to know my mistakes.so i did search this topic and i didnt find because my search title wasnt right like u said – Babak Abadkheir Aug 08 '16 at 10:18

3 Answers3

11

I suggest the datetime module, which is more practicale in this case (as polku suggested). You will then directly access only hour and minute to check if it is lunch time.

Further information can be found here: How to get current time in python and break up into year, month, day, hour, minute?

import datetime
now = datetime.datetime.now()

if now.hour == 14 and now.minute == 12:
    print ('its time to eating lunch')
else :
    print ('its not the time for eating lunch')
Community
  • 1
  • 1
Nikolas Rieble
  • 2,416
  • 20
  • 43
  • 1
    Except that you want to check the hour is 14, not 2. – Daniel Roseman Aug 08 '16 at 09:45
  • 1
    In many cases where a certain time is required, it is better to check if the time has already passed and then to set a respective flag than to check if the time is reached exactly. This is because it might well be that the if statement is not executed exactly at that time. – Michael Westwort Aug 08 '16 at 11:19
-1
import datetime as da

lunch = "02:12 PM"

now = da.datetime.now().strftime("%I:%M %p")

if now == lunch:

  print("lunch time")

else:

  print("not lunch time")
jagpreet
  • 53
  • 1
  • 10
-1

What if we want to do something like:-

if local_time == between 12:00 and 12:30 :
    print ('its time to eating lunch')
else:
     
if local_time == between 6:30 and 7:00 :
   print ('its time to eat dinner')
else:

I mean, it's not correct, but you get the idea

Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21
adam
  • 1