0

I've been trying to write a code that checks whoever clicked on a button first, and to see how much faster he was (let's say one player clicks 'z' and the other clicks '/'). I'm also trying to make it that if the difference is more than 5 seconds, the code should break out of comparison. I'm struggling with the way to exit that part.

I've tried using this to see how to extend threading.Timer to return something after 5 secs in order to break out of while True loop, and I've checked that, but I couldn't get the results I want. Here is the most recent part I've come up with:

import keyboard
from datetime import *
from time import time as timeepoch

def quiztry():
    player1 = input('Player 1 name: ')
    player2 = input('Player 2 name: ') 
    x = ''
    while x not in {'z','/'}:
        x = keyboard.read_key()
        if x == 'z':
            player1_click_time = datetime.now()   
            print(f'{player1} is ready to answer!')   
            timeout = timeepoch() + 5
        
            while timeepoch() < timeout:
                if keyboard.read_key() == '/':
                    print(f'{player2} is ready to answer!')         
                    player2_click_time = datetime.now()
                    diff = abs(player1_click_time - player2_click_time)
                    #here are some conditions and prints for various differences in time
        elif x == '/':         
            player2_click_time = datetime.now()
            print(f'{player2} is ready to answer!')
            timeout = timeepoch() + 5
        
            while timeepoch() < timeout:
                if keyboard.read_key() == 'z':
                    print(f'{player1} is ready to answer!')
                    player1_click_time = datetime.now()   
                    diff = abs(player1_click_time - player2_click_time)
                    #again some conditions and prints for various differences in time
    x = ''
Kropiciel
  • 23
  • 4

0 Answers0