口算题卡升级版本
import random
count = 0
strA = ""
strB = ""
while True:
# 随机生成1或者2,表示是+还是-
addOrreduce = random.randint(1, 2)
# 随机生成两个数字
a = random.randint(10, 200)
b = random.randint(10, 200)
if addOrreduce == 1:
if a + b > 100:
continue
strA = strA + "\n" + str(a) + "+" + str(b) + "="
strB = strB + "\n" + str(a) + "+" + str(b) + "=" + str(a + b)
else:
if a - b < 0:
continue
strA = strA + "\n" + str(a) + "-" + str(b) + "="
strB = strB + "\n" + str(a) + "-" + str(b) + "=" + str(a - b)
count = count + 1
if count == 1000:
break
# 写文件
with open('c:/学生用卷.txt', 'w',encoding='utf-8') as f:
f.write(strA)
with open('c:/家长用卷.txt', 'w',encoding='utf-8') as f:
f.write(strB)