import random
import operator
def test(n):
correct = 0
wrong = 0
L = []
while (correct + wrong) < n:
for i in range(2):
num = random.randint(1,20)
L.append(num)
L.sort(reverse = True)
op = random.choice("+-+")
#
加法大于
20
,将被重置,所以随机选择运算符的时候,增加加号被选中的机会
if op == "+":
answer = operator.add(L[0],L[1])
elif op == "-":
answer = operator.sub(L[0],L[1])
if answer > 20:
L = []
continue
else:
result = input("\n{} {} {} =
".format(L[0],op,L[1]))
result = int(result)
L = []
if result == answer:
print("
回答正确
")
correct += 1
else:
print("
回答错误
")
wrong += 1
L = []
print("\n
共计
{}
道题,回答正确
{}
题,回答错误
{}
题
".format(n,correct,wrong))
test(10)