Python Forum
Checking if the user is IDLE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking if the user is IDLE
#1
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
Reply
#2
The only methods i can think of are checking for mouse movement, keyboard input, or just a timeout.
Recommended Tutorials:
Reply
#3
(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
Reply
#4
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:
Output:
λ python time_out.py Your Dice rolls are (2, 4) What's the sum,You have 5 seconds to answer: Sorry, times up <Enter to exit> λ python time_out.py Your Dice rolls are (3, 4) What's the sum,You have 5 seconds to answer: 7 Correct the sum was 7 λ python time_out.py Your Dice rolls are (2, 5) What's the sum,You have 5 seconds to answer: 8 8 is wrong,corrcet was 7
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 3,316 Sep-14-2018, 07:54 PM
Last Post: Larz60+
  Python IDLE 3.6.2 on WIn7 vs Pyhton 3 IDLE raspberry djdan_23 5 5,708 Sep-07-2017, 12:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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