python3之二年级上数学练习题生成
二年级上数学练习题生成
作为一个家长不容易啊
1 1 #coding:utf-8 2 2 import random 3 3 #小学二年级上数学练习(100以内加减乘) 4 4 #生成的题数 5 5 count = 50 6 6 #每行题数 7 7 lineNum = 5 8 8 while count > 0: 9 9 #获取符号1为加,2为减,3为乘 10 10 operator = random.randint(1,3) 11 11 #获取操作数和操作结果 12 12 str = "" 13 13 num1 = num2 = result = 0 14 14 if operator == 3: #乘法口诀 15 15 num1 = random.randint(1,9) 16 16 num2 = random.randint(1,9) 17 17 result = num1*num2 18 18 str = "X" 19 19 elif operator == 2: #减法 20 20 num1 = random.randint(2,100) 21 21 num2 = random.randint(1,num1) 22 22 result = num1 - num2 23 23 str = "-" 24 24 else: #加法 25 25 num1 = random.randint(1,100) 26 26 num2 = random.randint(0,100-num1) 27 27 result = num1 + num2 28 28 str = "+" 29 29 print("%d%s%d=\t\t" %(num1,str,num2),end = "") 30 30 count -= 1 31 31 if count%5 == 0: 32 32 print()