import random

opr = ['+','-','×','÷']

num = [1,2,3,4,5,6,7,8,9,1/2,1/3,1/4,1/5,1/6,1/7,1/8,1/9,2/3,2/5,2/7,2/9,3/4,3/5,3/7,3/8,4/5,4/7,4/9,5/6,5/7,5/8,5/9,6/7,7/8,7/9,8/9]

jg = '0'

m1 = input("Input the number of problems you want: ")

try:

m = int(m1)

n = 0

while n <= m:

    fh = random.randint(0, 3)

    from random import choice

    n1 = choice(num)

    n2 = choice(num)

    rjg = 0

    if fh == 0:

        rjg = n1 + n2

    elif fh == 1:

        n1,n2 = max(n1,n2),min(n1,n2)

        rjg = n1 - n2

    elif fh == 2:

        rjg = n1 * n2

    elif fh == 3:

        n1,n2 = max(n1,n2),min(n1,n2)

        while n1 % n2 != 0:

            n1 = random.randint(1, 10)

            n2 = random.randint(1, 10)

            n1,n2 = max(n1,n2),min(n1,n2)

        rjg = int(n1 / n2)

        print(n1, opr[fh], n2, '= ')

        
        print('the right answer is', rjg)
        
    n = n+1

    if n > m:
        input()

except ValueError as e:
print("Invalid input.Please reset the program and input a number to run it correctly.")

我的代码仍不完全,没有完全实现作业要求(https://www.cnblogs.com/jiel/p/4810756.html)
目前我做到的有:控制题目生成的数量,计算出题目的答案,判断输入数据是否有意义并在无意义时提出指引。
没能做到,但是理论上已经有思路的有:随机运算符的数量(方法同我使用的随机符号,可再添加一个随机项,设置一个变量,每次循环过后该变量加一,直到与随机得出的变量相等,循环停止),扩大数字的范围(用编程语言实现对真分数的引入,设置变量控制数值范围),对输入答案和正确答案的评判(增加变量,使用=!判断,输出对错即可),确保不会出现负数等(在写出题目前判断结果是否是真分数或正数,若不是则不写出这个题目)
目前我认为自己不能做到的有:无法将题目,答案和统计对错结果另存为TXT文件,支持一万道题目的生成(数据有限)

 posted on 2021-11-21 03:35  20211418  阅读(42)  评论(0编辑  收藏  举报