Checking if the user is IDLE - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Checking if the user is IDLE (/thread-12905.html) |
Checking if the user is IDLE - Owenix - Sep-18-2018 Hey guys, I'm trying to make a function that detects if the user is "afk" for about 5 seconds, and than exits the code. would like some help :) import random import time import math def Dice(): dice = random.randint(1, 6) dice2 = random.randint(1, 6) return(dice, dice2); def Start(x): y = int(x) + int(5) #Adding 5 seconds to the time user_input = input(">>> ") if user_input: print("Your Dice rolls are", Dice()) Start(math.floor(time.time())) while y < math.floor(time.time()): #This doesn't work print("Its too late") exit() def Ready(): print("To start send any value") Start(math.floor(time.time())) Ready()Thanks RE: Checking if the user is IDLE - metulburr - Sep-18-2018 The only methods i can think of are checking for mouse movement, keyboard input, or just a timeout. RE: Checking if the user is IDLE - Owenix - Sep-18-2018 (Sep-18-2018, 12:31 PM)metulburr Wrote: The only methods i can think of are checking for mouse movement, keyboard input, or just a timeout. What's a timeout? and can't i make a loop based on prerequisite time > current time? Thanks for the answer RE: Checking if the user is IDLE - snippsat - Sep-18-2018 Can use Timer from threading. Your indentation is mixed,it's always 4-space. PEP-8 eg function is lowercase,underscores for longer names. Example: from threading import Timer import time, sys import random def dice(): dice = random.randint(1, 6) dice2 = random.randint(1, 6) return(dice, dice2) def start(timeout, dice): t = Timer(timeout, print, ['\nSorry, times up <Enter to exit>']) t.start() dice_trow = dice() print(f"Your Dice rolls are {dice_trow }") user_input = input(f"What's the sum,You have {timeout} seconds to answer: ") t.cancel() try: if int(user_input) == sum(dice_trow): print(f'Correct the sum was {sum(dice_trow )}') else: print(f'{user_input} is wrong,corrcet was {sum(dice_trow )}') except ValueError: pass if __name__ == '__main__': timeout = 5 start(timeout, dice)Test:
|