random.choice

发布于 2020-05-30  524 次阅读


from decimal import Decimal
import random, re

rd = re.compile(r'^-?\d*(?:\.\d*)?\d$')
Norm = Decimal('0.01')

def randint(a=0, b=10):
    return str(random.randint(a,b))

def _add():
    a, b = randint(), randint()
    return f'{a} + {b} =', Decimal(a) + Decimal(b)

def _sub():
    a, b = randint(), randint()
    return f'{a} - {b} =', Decimal(a) - Decimal(b)

def _mul():
    a, b = randint(), randint()
    return f'{a} * {b} =', Decimal(a) * Decimal(b)

def _dev():
    a, b = randint(), randint(1)
    return f'{a} / {b} =', (Decimal(a) / Decimal(b)).quantize(Norm, rounding = 'ROUND_HALF_UP')

opset = (_add, _sub, _mul, _dev)
def _op():
    return random.choice(opset)()

total = correct = 0
al = []
ct = ''
while True:
    if ct != 'y':
        expression, answer = _op()
    else:
        ct = ''
    ua = input(f'第{total+1}题:  {expression} ').replace(' ','')
    if rd.match(ua):
        total += 1
        if Decimal(ua).quantize(Norm, rounding = 'ROUND_HALF_UP') == answer:
            correct += 1
            al.append(f'第{total}题: {expression} {ua} 正确!')
            #print("此题回答正确!" )
        else:
            al.append(f'第{total}题: {expression} {ua} 错误!标准答案是 {answer}')
            #print(f"此题回答错误!标准答案是 {answer}" )
    else:
        while True:
            ct = input('输入错误,是否继续(y/n)').strip().lower()
            if ct in 'yn':
                break
        if ct == 'n':
            break

if total > 0:
    print(f'\n正确率为 {correct/total*100:.2f}%')
    print('以下是答题结果:')
    print(*al, sep='\n')
else:
    print('\n尚未回答任何问题!')

医学生