《python编程快速上手——让繁琐工作自动化》8.6.2 编写自己的乘法测试

《python编程快速上手——让繁琐工作自动化》8.6.2 编写自己的乘法测试,第1张

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)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/922154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存