创作灵感:最近把小学生的口算题从2位数改到3位数,100以内四则运算练习(千纬数学)再次更新,选取难题-优快云博客要不断刷题目,以前100以内的加减乘除也是这样刷出来的,代码如下:
import sqlite3
import random
from time import time
from pathlib import Path
#导入必要的库
resources_folder = Path(__file__).joinpath("../resources/").resolve()
db_filepath = resources_folder.joinpath("qwsx.db")
#db_filepath = '/storage/emulated/0/Pictures/qwsx.db'
#上面是数据库的存放位置,生成手机app请参考我以前的文章
#oppo版 需要用电脑调试应删除下面5行
def gettid(s1,fh,s2,dan):
conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
c = conn.cursor()
#如果公式存在,提取tid
cursor = c.execute("SELECT count(tid) from ys where s1 = ? and fh = ? and s2 = ?;", (s1,fh,s2,))
row = cursor.fetchone()
ctid = row[0]
#如果公式不存在,插入公式到数据库
if ctid == 0:
c.execute("INSERT INTO ys(s1,fh,s2,dan,cs,cuo) VALUES (?,?,?,?,0,0);", (s1,fh,s2,dan,))
conn.commit()
cursor = c.execute("SELECT tid from ys where s1 = ? and fh = ? and s2 = ? order by tid desc;", (s1,fh,s2,))
row = cursor.fetchone()
tid = row[0]
c.close()
conn.close()
return(tid)
#获取tid,题目的id
def settm(nd):
if nd ==1:
jj = random.randint(0,1)
elif nd ==2:
jj = random.randint(0,3)
if jj == 0:
#为加法
s1 = random.randint(0,100)
s2 = random.randint(0,(100 - s1))
cvalue = str(s1) + "+" + str(s2) + "="
dan = s1 + s2
hd = 1
tid = gettid(s1,jj,s2,dan)
ii = random.randint(0,4) #0为提交答案
if ii == 2:
cvalue = "□+" + str(s2) + "=" + str(dan) + ",□为"
dan = s1
elif ii == 3:
cvalue = str(s1) + "+□=" + str(dan) + ",□为"
dan = s2
elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
dan = jj
hd = 4 #hd4为符号
elif jj ==1:
s1 = random.randint(0,100)
s2 = random.randint(0,s1)
cvalue = str(s1) + "-" + str(s2) + "="
dan = s1 - s2
hd = 1
tid = gettid(s1,jj,s2,dan)
ii = random.randint(0,4) #0为提交答案
if ii == 2:
cvalue = "□-" + str(s2) + "=" + str(dan) + ",□为"
dan = s1
elif ii == 3:
cvalue = str(s1) + "-□=" + str(dan) + ",□为"
dan = s2
elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
dan = jj
hd = 4 #hd4为符号
elif jj ==2:
#乘法
s1 = random.randint(1,10)
s2 = random.randint(0,int(100 / s1))
cvalue = str(s1) + "×" + str(s2) + "="
dan = s1 * s2
hd = 1
tid = gettid(s1,jj,s2,dan)
ii = random.randint(0,4) #0为提交答案
if ii == 2:
cvalue = "□×" + str(s2) + "=" + str(dan) + ",□为"
da