1
while 1:
    wat=water()
    if wat==10:
        print("water condition")
        mixer.music.load("water.mp3")
        mixer.music.play()
        first=input("Drank?Y/N")
        if first.lower()=="y":
            with open("HealthLog.txt","a") as water1:
                Content=f"Drank water at [{getdate()}] \n"
                water1.write(Content)
        else:
            pass

Is there any way to wait for a couple of minutes and if no input is provided, then take the value "n" as input for the first variable?

Guess by default it will wait indefinitely. I tried using a timer function, but it cannot record any input.

What I am trying to do is to track my activities, so if I drink water I say y--> this records my activity and writes it to a file.

All help will be greatly appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sanketh s
  • 345
  • 1
  • 5
  • 14
  • Does this answer your question? [Keyboard input with timeout?](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) – rdas Jul 05 '20 at 15:54
  • @AnnZen `SIGALRM` is specific to Unix-like OSs only – rdas Jul 05 '20 at 16:03
  • The answer given by AnnZen is working perfectly fine. I am happy with the solution given by AnnZen – sanketh s Jul 06 '20 at 19:49

1 Answers1

1

Here is how you can use a combination of pyautogui.typewrite, threading.Thread and time.sleep:

from pyautogui import typewrite
from threading import Thread
from time import sleep

a = ''

def t():
    sleep(5)
    if not a: # If by 5 seconds a still equals to '', as in, the user haven't overwritten the original yet
        typewrite('n')
        typewrite(['enter'])

T = Thread(target=t)
T.start()

a = input()
b = input() # Test it on b, nothing will happen

Here is the code implemented into your code:

from pyautogui import typewrite
from threading import Thread
from time import sleep

while 1:
    wat = water()
    if wat == 10:
        print("water condition")
        mixer.music.load("water.mp3")
        mixer.music.play()
        
        first = 'waiting...'
        def t():
            sleep(5)
            if first == 'waiting...':
                typewrite('n')
                typewrite(['enter'])
        T = Thread(target=t)
        T.start()
        
        first = input("Drank?Y/N")
        if first.lower() == "y":
            with open("HealthLog.txt","a") as water1:
                Content=f"Drank water at [{getdate()}] \n"
                water1.write(Content)
        else:
            pass
Red
  • 26,798
  • 7
  • 36
  • 58