import time, random
tries= 0
correct = 0
numberOfQuestion = 10
for i in range(1, numberOfQuestion+1):
num1 =random.randint(0, 9)
num2 = random.randint(0, 9)
while True:
print(f'#{i}: {num1} * {num2} = ?')
T1 = time.time()
answer = input('>>>')
T2 = time.time()
total_time = T2-T1
if answer.isdigit():
answer = int(answer)
if total_time > 8:
print('Time is out!')
break
if answer != num1 * num2:
print('incorrect!')
tries +=1
if tries > 2:
print('Out of tires')
break
if answer == num1 * num2:
print('correct!')
time.sleep(1)
correct += 1
break
print(f'Score: ', correct/numberOfQuestion)
《python编程快速上手——让繁琐工作自动化》8.6.2 编写自己的乘法测试
最新推荐文章于 2022-08-23 17:10:11 发布
这个Python程序设计了一个数学速算游戏,玩家需要在限定时间内回答乘法问题。程序生成两个0到9之间的随机数,然后等待用户输入答案。如果用户在8秒内正确回答,计为正确;超过时间或回答错误则会提示并继续下一轮。游戏共进行10轮,每轮结束后显示正确率。
摘要由CSDN通过智能技术生成