代码比较简单,写代码的主要原因是,是小学老师每天要求出一份题目,20以内的随机算式。我实在不想动脑子天天闹这个事情。所有代码直接生成,打印出来给孩子做就可以了。
直接上代码,就当练习一下基本功。同时也参考了部分博主的代码,稍微修改了一下,都是找的免费的,公开发布在博客上面的代码,而且是解决小学水平的问题,应该不涉及版权吧,O(∩_∩)O哈哈~,如果涉及到您的版权,联系我立马删除。
import time
import random
def create01(n,m): #产生数字m以内 n个算式
numbers=1
list_equation = []
while numbers<=n:
add1=random.randint(11,21)
add2=random.randint(2,21)
ops1=ops[0]
eq=str(add1)+ops1+str(add2)
val=eval(eq)
if add1-add2>=0 and add1+add2<=m and add1!=add2:
#if add1%add2==0 and add1*add2<=m : #这是乘除法出题 需要修改ops=['*','/']
#continue
#print('第',numbers,'题')
time.sleep(0)
#print(add1,ops1,add2,'=')
#question=question='{} {} {} {}'.format(add1,ops1,add2,'=') #这是随机加减法 下面改成只出减法 自行修改即可
question=question='{} {} {} {}'.format(add1,'-',add2,'=')
if question not in list_equation:
list_equation.append(question)
#ask=int(input('答案是几:'))
#if ask==val:
#print('恭喜你,答对了')
#print()
#numbers+=1
#random.shuffle(ops)
#else:
#print('答错了,正确答案是',val)
#print()
numbers+=1
#print(numbers-1)
random.shuffle(ops)
for i in list_equation[:20]: #循环nums次 写入前20个式子
with open ('出题.txt','a') as f:
f.write(i+'\n\n')
print('两数加减法保存在:出题.txt 文件中')
def create02(n,m): #产生数字m以内 n个算式
list_equation = []
numbers=1
while numbers<=n:
add1=random.randint(2,21)
add2=random.randint(2,21)
add3=random.randint(2,21)
add4=random.randint(2,21)
ops1=ops[0]
#eq=str(add1)+ops[random.randint(0,1)]+str(add2)
#val=eval(eq)
if add1-add2>=0 and add1+add2<=m and add3-add4>=0 and add3!=add2 and add3!=add1 and add1!=add4 and add2!=add4 and add3+add4<=m:
#if add1%add2==0 and add1*add2<=m and add3%add4==0 and add3*add4<=m: #这是乘除法出题 需要修改ops=['*','/']
#continue
#print('第',numbers,'题')
time.sleep(0)
#print(add1,random.sample(ops,1)[0],add2,'比较',add3,random.sample(ops,1)[0],add4,)
#ask=int(input('答案是几:'))
#if ask==val:
#print('恭喜你,答对了')
#print()
#numbers+=1
#random.shuffle(ops)
#else:
#print('答错了,正确答案是',val)
#print()
numbers+=1
#print(numbers-1)
#print(add1,random.sample(ops,1)[0],add2,'比较',add3,random.sample(ops,1)[0],add4,)
question='{} {} {} {} {} {} {}'.format(add1,random.sample(ops,1)[0],add2,'比较',add3,random.sample(ops,1)[0],add4)
if question not in list_equation:
list_equation.append(question)
random.shuffle(ops)
for i in list_equation[:20]: #循环nums次 写入前20个式子
with open ('出题.txt','a') as f:
f.write(i+'\n\n')
print('两数加减法比大小保存在:出题.txt 文件中')
def create03(nums):
"""
:param nums: 需要生成个数
:return: 返回元组,含等式和答案
"""
#存储等式
list_equation = []
#存储答案
list_result = []
for i in range(nums):
#jishutansuozhe判断计算结果,不能为负值,若为负值,就重新生成
result = -1
while result < 0:
# 随机生成第1位,20以内
first = random.randint(1, 20)
# 随机生成第1个加减符号
first_symbol = random.choice(["+", "-"])
# 随机生成第2位,20以内
second = random.randint(1, 10)
# 随机生成第2个加减符号
second_symbol = random.choice(["+", "-"])
# 随机生成第3位,20以内
three = random.randint(1, 10)
if first!=second and first!=three and second!=three :
equation = str(first) + first_symbol + str(second) + second_symbol + str(three) + "="
if first_symbol == "+":
if second_symbol == "+":
result =first + second + three
else:
if second - three>0:
result = first + second - three
else:
if second_symbol == "+":
if first - second>0:
result =first - second + three
else:
if first - second>three and first > second :
result = first - second - three
if result > 0 and result<=20:
#判断是否重复
if equation not in list_equation:
list_equation.append(equation)
list_result.append(equation+str(result))
break
for i in list_equation[:20]: #循环nums次 写入前20个式子
with open ('出题.txt','a') as f:
f.write(i+'\n\n')
print('3数加减法保存在:出题.txt 文件中')
print(list_equation[:20])
return (list_equation,list_result)
if __name__ == '__main__':
with open ('出题.txt','a') as f:
f.truncate(0) #清空文件
ops=['+','-']
#ops=['*','/']
#print(random.sample(ops,1)[0])
random.shuffle(ops)
ops1=ops[0]
random.shuffle(ops)
ops2=ops[0]
n = 20 #出200个题目 最后输出20个
m = 20 #出20以内加减法
#随机生成2个数加减算式
create01(n,m)
#随机生成2项加减比大小
create02(n,m)
#随机生成32个数加减算式
create03(200)#循环200次 获得前20个式子
time.sleep(3)