如何在Python循环中添加计时器而不使用PyInputPlus(没有async或threading)


import random
import time
num_questions = 10
score = 0

print('''
Welcome to the math quiz.
You will have 3 tries and 8 seconds to answer each question.
Good luck.
''')
time.sleep(3)
for question_num in range(num_questions):
max_chances = 3
current_chances = 0
num1 = random.randint(0, 9)
num2 = random.randint(0, 9)
correct_answer = num1 * num2
while current_chances < max_chances:
current_chances += 1
user_answer = input('#%s: %s * %s = ' % (question_num+1, num1, num2))
if int(user_answer) == correct_answer:
print('Correct.')
break
else:
if current_chances == max_chances:
print('Out of tries. Please wait for the next question.')
time.sleep(1)
else: 
print('Not quite. Try again.')
continue

我有一个程序,问10个数学问题,每个问题3次。这是我正在做的一个练习项目,我在用python自动化无聊的东西,其中的说明是:

要查看PyInputPlus为您做了多少工作,请尝试在不导入的情况下自己重新创建乘法测验项目。这个程序将提示用户10道乘法题,范围从0 × 0到9 × 9。您需要实现以下功能:•如果用户输入正确的答案,程序显示"正确!"一秒钟,然后继续下一个问题。•用户有三次输入正确答案的机会节目继续下一个问题。•第一次显示问题后8秒,问题是即使用户输入正确答案后,也被标记为错误8秒的限制

我相信我已经满足了所有的要求,除了8秒的限制。我不太确定如何将其实现到我的代码中。这本书还没有介绍asyncio或线程,我的一些朋友已经提出了一些想法,我也不确定还有什么其他的方法可以为这个程序实现一种定时器。有人能帮忙吗?谢谢你。

我认为如果不为定时器创建另一个线程,就没有办法做到这一点。因为你必须同时做两个进程(计时器和输入)。

我认为threading.Timer对你的情况有用。这很容易。首先,您必须创建一个定时器:

t = threading.Timer(8,print,["Your time is up"])

超时时间8秒,当它结束时,它将打印指定的消息。然后,必须在输入之前启动计时器。在输入,可以取消你的计时器。所以你的输入语句就像这样:

timer = threading.Timer(8,print,["Your time is up"]) # creating timer
timer.start() # starting timer
user_answer = input('#%s: %s * %s = ' % (question_num+1, num1, num2))
timer.cancel() # cancelling 

注意:当'your time is up'消息出现时输入仍然在继续,它只是打印消息。也有一些方法可以终止它,但它们更复杂,并且依赖于操作系统。检查这个答案。

你的代码应该是这样的:


for question_num in range(num_questions):
max_chances = 3
current_chances = 0
num1 = random.randint(0, 9)
num2 = random.randint(0, 9)
correct_answer = num1 * num2
while current_chances < max_chances:
current_chances += 1
timer = threading.Timer(8,print,["Your time is up"]) # creating timer
timer.start() # starting timer
user_answer = input('#%s: %s * %s = ' % (question_num+1, num1, num2))
timer.cancel() # cancelling 
# user_answer likely to be empty, but, still user could write something,
# because we are not terminating input process, just printing a message.
if user_answer == ""
if int(user_answer) == correct_answer:
print('Correct.')
break
else:
if current_chances == max_chances:
print('Out of tries. Please wait for the next question.')
time.sleep(1)
else: 
print('Not quite. Try again.')
continue

使用time.time()函数

import time
correct=0
Qn=0
for i in range(10):
for j in range(10):
time1=int(time.time())#time when this iteration started
Qn+=1
for k in range(3):#3 tries
try:#as the user could enter a non int value
userAns=int(input(f'#{Qn}:{i}*{j}=?'))
time2=int(time.time())#time when the user entered some value
except ValueError:#if the user did not enter a number
if k!=2:#if the user is not on their 3rd try
'''if the user is not on their 3rd try as there is no need to say enter a number after 
the last try because the program would have already moved on'''
print('Enter a number!')
continue#to next try
else:
if time2-time1>=8:#if the user took more than or equal to 8 seconds to answer
print('Timeout!')
break#to the next number
if userAns==i*j:
print('Correct!')
time.sleep(1)
correct+=1
break#to the next number
else:
print('Incorrect!')

else:
print('Out of tries!')
'''the for loop ended natuarally meaning user failed to
enter the correct answer in the given no. of tries'''
print('score:%s/%s'%(correct,100)) 

最新更新