Sep-18-2018, 03:06 PM
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:
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