1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import random
random.seed(0)
def calculator(n, maximum): """随机产生n道正整数四则运算的题目,用户输入计算结果, 判断输入正确与否,并统计正确率。题目保证减法不出现负数.""" correct = 0 for i in range(n): b = random.randint(0, maximum) a = random.randint(b, maximum) sign = random.choice("+-*/") print(f"{a}{sign}{b}=", end="") result = int(input()) if result == eval(f"{a}{sign}{b}"): print("恭喜你,回答正确") correct = correct + 1 else: print("回答错误,你要加油哦!") print(f"答对{correct}题,正确率为{correct / n * 100}%")
if __name__ == "__main__": num = int(input("请输入出题数量:")) m = int(input("请输入参与计算的最大数字:")) calculator(num, m)
|