原创来自https://www.jianshu.com/p/86e3d5b21eba
import random
ROW = 26 # 表格的行数
COL = 5 # 表格的列数
COUNT = ROW * COL # 行×列,一张试卷总共题目的数量
Answer100 = [] # 数组初始化
for i in range(0, COUNT):
Answer100.append(0)
# n 是表格的行数,通过 n 可以方便计算数组下标,把答案写入Answer100[]中
def printLine(n):
output = '| ' # 该行中第一个 “|”
count = 0
while (count<COL):
a = random.randint(1, 20)
b = random.randint(1, 20)
c = random.randint(1,20)
op1 = random.randint(0, 1) # +/-
op2 = random.randint(0, 1)
if op1 == 0 and op2==1 and (a + b-c) < 20 and (b>c) and (b-c)>0: # ADD
# print('%d + %d = ' % (num1, num2), ' | ', end='')
count += 1
Answer100[n * COL + count - 1] = a + b - c
output += str(a) + ' + ' + str(b) + ' - ' + str(c) + ' = | '
if op1 == 1 and op2 == 0 and (a > b) and (a - b) > 0 and (a - b + c) < 20: # SUB
# print('%d - %d = ' % (num1, num2), ' | ', end='')
count += 1
Answer100[n * COL + count - 1] = a - b + c
output += str(a) + ' - ' + str(b) + ' + '+ str(c) + ' = | '
if op1 == 0 and op2 == 0 and (a + b + c) < 20: # SUB
# print('%d - %d = ' % (num1, num2), ' | ', end='')
count += 1
Answer100[n * COL + count - 1] = a + b + c
output += str(a) + ' + ' + str(b) + ' + '+ str(c) + ' = | '
if op1 == 1 and op2 == 1 and (a > b) and (a - b) > 0 and (b > c) and (b - c) > 0 and (a - b) > c and (a - b - c) < 20: # SUB
# print('%d - %d = ' % (num1, num2), ' | ', end='')
count += 1
Answer100[n * COL + count - 1] = a - b - c
output += str(a) + ' - ' + str(b) + ' - '+ str(c) + ' = | '
output += '\n'
return output
with open("简单运算.md", 'w') as f: # 写到硬盘中的 简单运算.md 文件中
f.write(printLine(0)) # 表格标题行
f.write('| ------- | ------- | ------- | ------- | ------- |\n')
# 表格分隔行,全是字符
# 其实这里应该根据 COL 的数量来判断中间几个 “----” 的
# 但是好像在Typora 中,这个错了不影响表格,那就这么样吧,懒得弄了
for i in range(1, ROW): # 写后面的内容行
f.write(printLine(i))
# 答案写在另一页,先来个二级标题
f.write('## Answer \n')
# 答案的标题行
f.write('| ' + str(Answer100[0]) + ' | '
+ str(Answer100[1]) + ' | '
+ str(Answer100[2]) + ' | '
+ str(Answer100[3]) + ' | '
+ str(Answer100[4]) + ' | \n')
# 答案的分隔行
f.write('| ---- | ---- | ---- | ---- | ---- |\n')
# 答案剩下的行
for i in range(1, ROW):
f.write('| ' + str(Answer100[i * 5]) + ' | '
+ str(Answer100[i * 5 + 1]) + ' | '
+ str(Answer100[i * 5 + 2]) + ' | '
+ str(Answer100[i * 5 + 3]) + ' | '
+ str(Answer100[i * 5 + 4]) + ' | \n')
1.png
2.png
网友评论