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
#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


Messages In This Thread
Checking if the user is IDLE - by Owenix - Sep-18-2018, 12:27 PM
RE: Checking if the user is IDLE - by metulburr - Sep-18-2018, 12:31 PM
RE: Checking if the user is IDLE - by Owenix - Sep-18-2018, 12:41 PM
RE: Checking if the user is IDLE - by snippsat - Sep-18-2018, 03:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IDLE not importing pygame (ok outside of IDLE) miner_tom 1 3,386 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,793 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