所以我正在尝试进行数学测验,在3或4秒后弹出问题时,计算机会为你回答,但如果玩家在限时内回答计算机没有说出答案。我刚开始在学校编写一个项目...提前致谢
import time
import random
def computer():
time.sleep(5) #number of seconds the computer waits before answering
print (correct_answer)
#====================================================
from random import randint
number_1 = (randint(0,10)) #random number one
number_2 = (randint(0,10)) #random number two
print(number_1, '+', number_2, "="), #(when i tried to to as an input there was an error)
time.sleep(1) #after one second the question pops up
answer = int(input())
correct_answer = (number_1 + number_2)
if answer == correct_answer:
print ("Correct")
else:
print ("wrong")
答案 0 :(得分:0)
这实际上并不那么容易,因为input
函数暂停执行程序的其余部分以及time.sleep
。您可能需要getch
函数或类this来删除input
。然后你需要一个倒计时或递增计时器。您可以通过从当前时间(start_time
)
time.time()
来实现
import time
# start_time = current time
start_time = time.time()
timer = 0
while True:
timer = time.time() - start_time
print(round(timer, 2))
if timer > 3:
print('Too late. Next question.')
# New start_time = current time, to reset the timer.
start_time = time.time()