本次作业要求参见:[https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631]

结对伙伴:王可非

功能1. 四则运算

import random


def generate():

    ope = ['+', '-', '*', '/']
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    num3 = random.randint(1, 10)
    num4 = random.randint(1, 10)
    ope1 = random.choice(ope)
    ope2 = random.choice(ope)
    ope3 = random.choice(ope)
    exp = str(num1) + str(ope1) + str(num2) + str(ope2) + str(num3) + str(ope3) + str(num4)
    return exp


ret = []
for i in range(20):
    ret.append(generate())
k = 0
for el in ret:
    print(el)
    print('?', end="")
    answer = float(input())
    if answer == eval(el):
        k += 1
        print('答对啦,你真是个天才!')
    else:
        print('再想想吧,答案似乎是%d喔!' % (eval(el)))
print('你一共答对%d道题,共20道题。' % k)

功能2. 支持括号

    def generate(self):
        ope = ['+', '-', '*', '/']
        bra = ['(', '', ')']
        num1 = ru(0, 1)
        num1 = f(num1).limit_denominator(10)
        num2 = ru(0, 1)
        num2 = f(num2).limit_denominator(10)
        num3 = random.randint(1, 10)
        num4 = random.randint(1, 10)
        ope1 = random.choice(ope)
        ope2 = random.choice(ope)
        ope3 = random.choice(ope)

        left1 = random.randint(0, 1)
        left2 = random.randint(0, 1)
        left3 = random.randint(0, 1)
        right1 = random.randint(1, 2)
        right2 = random.randint(1, 2)
        right3 = random.randint(1, 2)
        if left1 == 0:
            left2 = 1
            left3 = 1
            if right1 == 2:
                right2 = 1
                right3 = 1
            else:
                right2 = 2
                right3 = 1
        else:
            if left2 == 0:
                left3 = 1
                right1 = 1
                if right2 == 2:
                    right3 = 1
                else:
                    right3 = 2
            else:
                left3 = 0
                right1 = 1
                right2 = 1
                right3 = 2
        exp = bra[left1] + str(num1) + str(ope1) + bra[left2] + str(num2) + \
              bra[right1] + str(ope2) + bra[left3] + str(num3) + bra[right2] + \
              str(ope3) + str(num4) + bra[right3]
01-22 20:03