import random标签:op1,op2,randint,python,random,ans,100 From: https://www.cnblogs.com/xiaofeng0403/p/17695654.html
from random import choice
ops = ('+', '-', '×', '÷')
ans = []
i = 0
while i < 42:
op1 = choice(ops)
op2 = choice(ops)
n = random.randint(1, 9)
if op1 == '+' and op2 == '+':
a = random.randint(0, 100)
b = random.randint(0, 100 - a)
c = random.randint(0, 100 - a - b)
ans.append(a + b + c)
elif op1 == '+' and op2 == '-':
a = random.randint(0, 100)
b = random.randint(0, 100 - a)
c = random.randint(0, a + b)
ans.append(a + b - c)
elif op1 == '+' and op2 == '×':
b = random.randint(0, 9)
c = random.randint(0, 9)
a = random.randint(0, 100 - b * c)
ans.append(a + b * c)
elif op1 == '+' and op2 == '÷':
c = random.randint(1, 9)
b = n * c
a = random.randint(0, 100 - b / c)
ans.append(a + b / c)
elif op1 == '-' and op2 == '+':
a = random.randint(0, 100)
b = random.randint(0, a)
c = random.randint(0, 100 - a + b)
ans.append(a - b + c)
elif op1 == '-' and op2 == '-':
a = random.randint(0, 100)
b = random.randint(0, a)
c = random.randint(0, a - b)
ans.append(a - b - c)
elif op1 == '-' and op2 == '×':
b = random.randint(0, 9)
c = random.randint(0, 9)
a = random.randint(b * c, 100)
ans.append(a - b * c)
elif op1 == '-' and op2 == '÷':
c = random.randint(1, 9)
b = n * c
a = random.randint(100 - b / c, 100)
ans.append(a - b / c)
elif op1 == '×' and op2 == '+':
a = random.randint(0, 9)
b = random.randint(0, 9)
c = random.randint(0, 100 - a * b)
ans.append(a * b + c)
elif op1 == '×' and op2 == '-':
a = random.randint(0, 9)
b = random.randint(0, 9)
c = random.randint(0, a * b)
ans.append(a * b - c)
elif op1 == '÷' and op2 == '+':
b = random.randint(1, 9)
a = n * b
c = random.randint(0, 100 - a / b)
ans.append(a / b + c)
elif op1 == '÷' and op2 == '-':
b = random.randint(1, 9)
a = n * b
c = random.randint(0, a / b)
ans.append(a / b - c)
else:
continue
print("第%s题: %s%s%s%s%s=" % (i + 1, a, op1, b, op2, c))
i += 1
print("*" * 60)
i = 0
while i < len(ans):
print("第%s题的答案是:%d" % (i + 1, ans[i]))
i += 1