python混合运算_Python⾃动⽣成100以内加减乘除混合运算
题
import random
from random import choice
ops = ('+','-','×','÷')
ans = []
i=0
while i < 100 :
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)
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)
random pythonb = 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 == '-' :
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论