I’m creating a game in which you have to guess the result of an operation in a certain time; to do this i created a thread to have a timed input and then I close the thread after the time is up.
Issue
My problem is that when the user doesn’t enter any number, before the time is up, in the next cycle the input waits for two inputs instead of one.
So, what I need is to clear the stdin before getting a new input. I find some solutions for Unix but nothing for Windows.
Code
JavaScript
x
66
66
1
import os
2
import random
3
from time import sleep
4
import threading
5
import concurrent.futures
6
7
random.seed()
8
9
def get_input(a):
10
a[0]=input()
11
12
def do_operation(time, n):
13
number_1 = random.randint(0,n)
14
number_2 = random.randint(0,n)
15
operations = ['+', '-', '/', '*']
16
operation = random.choices(operations)
17
operation = "".join(operation)
18
if operation == '/':
19
while(number_1 % number_2 != 0):
20
number_1 = random.randint(0,n)
21
number_2 = random.randint(0,n)
22
#Trovo i risultati delle operazioni
23
if operation == '+':
24
result = number_1 + number_2
25
elif operation == '-':
26
result = number_1 - number_2
27
elif operation == '*':
28
result = number_1 * number_2
29
elif operation == '/':
30
result = number_1 / number_2
31
32
print(number_1,operation,number_2, '=', end = " ")
33
count = [None, result]
34
x = threading.Thread(target=get_input, args = (count,), daemon=True)
35
x.start()
36
x.join(time)
37
return count
38
39
40
def stampastats(lives, points):
41
print('nttttlives:', end = " ")
42
43
for live in range(lives):
44
print('*', end = " ")
45
46
print('nttttPoints:', points)
47
48
#main
49
lives = 3
50
points = 0
51
while(lives > 0):
52
stampastats(lives, points)
53
result = do_operation(10, 200)
54
if result[0] != result[1]:
55
print('nWrong')
56
print('nThe result was: ' + str(result[1]))
57
lives -= 1
58
else:
59
print('nBravo')
60
points += 1000
61
if lives == 0:
62
print('nGAME OVER')
63
else:
64
sleep(1)
65
os.system('cls')
66
Advertisement
Answer
There is a pending input()
if a timeout happened. You can send an Enter in this case; I’m applying the pyautogui
module to do that. (Call is_alive()
after join()
to decide whether a timeout happened.).
Some runtime errors fixed (and some debugging outputs kept) in the following script:
JavaScript
1
68
68
1
import os
2
import random
3
from time import sleep
4
import threading
5
import concurrent.futures
6
import pyautogui
7
8
random.seed()
9
10
def get_input(a):
11
a[0]=input()
12
13
def do_operation(time, n):
14
number_1 = random.randint(0,n)
15
number_2 = random.randint(0,n)
16
operations = ['+', '-', '/', '*']
17
operation = random.choices(operations)
18
operation = "".join(operation)
19
#Trovo i risultati delle operazioni
20
if operation == '+':
21
resulta = number_1 + number_2
22
elif operation == '-':
23
resulta = number_1 - number_2
24
elif operation == '*':
25
resulta = number_1 * number_2
26
else: # operation == '/':
27
while(number_2 == 0 or number_1 % number_2 != 0):
28
number_1 = random.randint(0,n)
29
number_2 = random.randint(0,n)
30
resulta = number_1 / number_2
31
32
print(number_1,operation,number_2, '=', end = " ")
33
count = [None, resulta]
34
x = threading.Thread(target=get_input, args = (count,), daemon=True)
35
x.start()
36
x.join(time)
37
#print( x.native_id, x.is_alive(), x.isDaemon()) # debugging info
38
if x.is_alive():
39
pyautogui.press("enter") # answer to pending input()
40
return count
41
42
43
def stampastats(lives, points):
44
print('nttttlives:', end = " ")
45
for live in range(lives):
46
print('*', end = " ")
47
print('nttttPoints:', points)
48
49
#main
50
lives = 3
51
points = 0
52
while(lives > 0):
53
stampastats(lives, points)
54
result = do_operation(10, 200)
55
#print(result)
56
if((result[0] == None) or (result[0] == '') or (float(result[0]) != float(result[1]))):
57
print('nWrong', result)
58
print('The result was: ' + str(result[1]))
59
lives -= 1
60
else:
61
print('nBravo', result)
62
points += 1000
63
if(lives == 0):
64
print('nGAME OVER')
65
else:
66
sleep(1)
67
# os.system('cls')
68