提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
设计一个Python程序
这个程序每次运行的时候,一开始都会打印一张班级同学的概率表,概率表反映了同学被抽检实验作业的概率。打印完以后,程序会询问用户,是否要随机抽一名同学,用户如果选择是,程序就会随机选择一名同学,打印出学号,并让用户输入成绩,用户输入0-100范围的整数,然后程序会问用户是否需要再输入一个学生的成绩,如果是,则输入学号后2位,输完以后再输成绩。然后程序会回到之前询问用户是否要随机抽一名同学。
a. 程序每次运行只要有抽到同学,需要用数据库把抽到的同学的学号,成绩,抽到的日期输入到数据库中,为每一次程序运行计算概率表提供数据基础
b. 计算概率表每一个同学的概率的总原则是: 如果之前程序抽到并且输入过这个同学的成绩,成绩 >=60 ,下次程序运行,他的概率应该变小,反之变大。注意这里的之前不是指上一次,而是指所有之前运行的时候,也就是从第一次运行到现在的运行。至于每一次概率变小或者增大多少,和数据库中的历史数据的关系可以自己定义。
c. 数据库第一次构建时,每一个同学的概率应该都是一样的
d. 在程序运行过程中,已经抽到的同学应该在本次运行中被排除出去,这样保证不会重复抽到一样的同学
e. 概率可以通过调整抽签的范围来生成,比如3个红色的球和5个蓝色的球,抽到红色球的概率就是3 / (3 + 5)
一、sql
create table if not exists student(student_no integer NOT NULL primary key,score integer,date date,times integer,checks integer)
#创建一个表,若表不存在的时候创建这个表,表中设置了的字段主要为学生学号(int值,不为空,主键),成绩,插入时间,可以被抽到的次数和上一次是否被抽到
insertOneStudent="insert into {}(student_no,score,date,times,checks) values(?,?,?,?,?)".format(dbTables["name"]) #插入学生的基本信息
getStudentSql = "select score from {} where student_no=?".format(dbTables["name"]) #查找学生的成绩
getStudentTimesSql = "select times from {} where student_no=?".format(dbTables["name"]) #查找学生的成绩
getStudentchecksSql = "select student_no from {} where checks=1".format(dbTables["name"]) #查找上一次被抽到的学生
updateStudentScoreSql="update student set score=? where student_no=?" #更新学生的成绩
updateStudentDateSql="update student set date=? where student_no=?" #更新学生的成绩更新日期
updateStudentTimesSql="update student set times=? where student_no=?" #更新学生被抽到的次数
updateStudentChecksSql="update student set checks=? where student_no=?" #更新学生上次是否被抽到
deleteStudentSql="delete from {} where student_no=?".format("student") 删除学生记录
二、写程序步骤
1.引入库
from os import name
import datetime
import sqlite3 as sql
import random
2.插入学生数据
for i in range(2017249001,2017249073):
cur.execute(insertOneStudent,(i,None,date,10,0))
cur.execute(deleteStudentSql,(2017249024,))
cur.execute(deleteStudentSql,(2017249038,))
cur.execute(deleteStudentSql,(2017249055,))
3.插入学生数据
for i in range(2017249001,2017249073):
cur.execute(insertOneStudent,(i,None,date,10,0))
cur.execute(deleteStudentSql,(2017249024,))
cur.execute(deleteStudentSql,(2017249038,))
cur.execute(deleteStudentSql,(2017249055,))
4.计算学生被抽到的概率
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
a=a+result[0][0]
print("下面是班级同学被抽到的概率表:")
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
c=(result[0][0])/a
print("学号为"+str(i)+"的学生被抽到的概率是%.2f"%c)
5.判断上一次抽到几位同学(程序中最多抽两位同学)
cur.execute(getStudentchecksSql) #得到表中学号对应checks值为1的值
studentCheck_equal1=cur.fetchall()
#print(studentCheck_equal1)
if(len(studentCheck_equal1)==1): #若studentCheck_equal1值为1 则执行此函数
studentCheck_equal1One=studentCheck_equal1[0][0]
def deleteStudent_no(n):
if(n!=studentCheck_equal1One):
return True
#print(len(studentCheck_equal1)) 返回列表长度
#若studentCheck_equal1值为 则执行此函数
if(len(studentCheck_equal1)==2):
studentCheck_equal1One=studentCheck_equal1[0][0]
studentCheck_equal1two=studentCheck_equal1[1][0]
def deleteStudent_no(n):
if(n!=studentCheck_equal1One):
return True
if(n!=studentCheck_equal1two):
return True
6.随机抽一位同学
将times与student_no配合起来,加入到列表中并进行打乱,抽出第一个元素
print("是否要随机抽一名同学(是 or 否,若想退出程序则出入exit)",end=" ")
checkStudent=input()
while(checkStudent!="是" and checkStudent!="exit" and checkStudent!="否" ):
print("输入内容不规范,请重新输入(是 or 否,若想退出程序则出入exit):" , end=" ")
checkStudent=input()
if(checkStudent=="exit" ):
break
if(checkStudent=="是"): #若选择是,则将所有学号根据times次数依次加入至studentList中,并进行打乱
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
for checkStudentNumber in range(0,result[0][0]):
studentList.append(i)
#print(studentList) 查看所有列表中的值
if(studentCheck_equal1!=[]): #将上次抽过的学生从列表中删除
studentList=filter(deleteStudent_no,studentList)
studentList=list(studentList)
#print(studentList)
random.shuffle(studentList)
print("抽到的学号为:"+str(studentList[0]))
7.给抽到的同学成绩
print("请给此学号的成绩(0-100):",end=" ")
studentScore=input()
#存在问题
# while(not(is_number(studentScore))): #若输入的值不符合规定,则需要重新输入
# print("您输入的成绩不规范,请重新输入(0-100):",end=" ")
# studentScore=input()
while(int(studentScore)<0 or (int(studentScore)>100)):
print("您输入的成绩不规范,请重新输入(0-100):",end=" ")
studentScore=input()
# print(not(is_number(studentScore)) and ((int(studentScore)>100) or (int(studentScore)<0)))
cur.execute(updateStudentScoreSql,(studentScore,studentList[0],))
cur.execute(updateStudentDateSql,(date,studentList[0],))
if(int(studentScore)>=60): #进行判定,若程序大于60分,则times值减一,若times值为1时,则不进行改变
cur.execute(getStudentTimesSql,(studentList[0],))
result=cur.fetchall()
if(result[0][0]==1):
break
times1=result[0][0]-1
cur.execute(updateStudentTimesSql,(times1,studentList[0],))
if(int(studentScore)<60): #进行判定,若程序大于60分,则times值加一
cur.execute(getStudentTimesSql,(studentList[0],))
result=cur.fetchall()
times1=result[0][0]+1
cur.execute(updateStudentTimesSql,(times1,studentList[0],))
8.自己选择学生并给予成绩
print("是否需要再输入一个学生的成绩(是 or 否,若想退出程序则出入exit):",end=" ")
inputStudentNumberAgain=input()
while(inputStudentNumberAgain!="是" and inputStudentNumberAgain!="exit" and inputStudentNumberAgain!="否"):
print("输入内容不规范,请重新输入(是 or 否,若想退出程序则出入exit):" , end=" ")
inputStudentNumberAgain=input()
if(inputStudentNumberAgain=="是"):
print("请输入此学生的学号后两位:",end=" ")
studentNumber1=input()
while((int(studentNumber1)>73) or (int(studentNumber1)<0)): #若输入的值不符合规定,则需要重新输入
print("您输入的学号不规范,请重新输入(1-72):",end=" ")
studentNumber1=input()
while(studentNumber1=="24" or studentNumber1=="38" or studentNumber1=="55"):
print("班级里没有此学号的学生,请重新输入",end="")
studentNumber1=input()
studentNumber2=int(studentNumber1)+2017249000
print("请输入此学生的成绩(0-100):",end=" ")
studentScore1=input()
while((int(studentScore1)>100) or (int(studentScore1)<0)): #若输入的值不符合规定,则需要重新输入
print("您输入的成绩不规范,请重新输入(0-100):",end=" ")
studentScore1=input()
#加上checks修改函数
if(int(studentScore1)>=60):
cur.execute(getStudentTimesSql,(studentNumber2,))
result=cur.fetchall()
if(result[0][0]==1):
break
times1=result[0][0]-1
cur.execute(updateStudentTimesSql,(times1,studentNumber2,))
if(int(studentScore1)<60):
cur.execute(getStudentTimesSql,(studentNumber2,))
result=cur.fetchall()
times1=result[0][0]+1
cur.execute(updateStudentTimesSql,(times1,studentNumber2,))
cur.execute(updateStudentScoreSql,(studentScore1,studentNumber2,))
cur.execute(updateStudentDateSql,(date,studentNumber2,))
if(checkStudent=="是"):
cur.execute(updateStudentChecksSql,(1,studentList[0],))
if(inputStudentNumberAgain=="是"):
cur.execute(updateStudentChecksSql,(1,studentNumber2,))
if(inputStudentNumberAgain=="exit"):
break
9.全部代码
from os import name
import datetime
import sqlite3 as sql
import random
dbPath="random1.db"
db=sql.connect(dbPath)
cur=db.cursor()
dbTables={ #创建字典类型dbTables,第一个键值对为表的名字,第二个是创建表的sql命令
"name":"student",
"createSql":"create table if not exists student(student_no integer NOT NULL primary key,score integer,date date,times integer,checks integer)"
}
date=datetime.datetime.now() #date表示当前时间
a=0 #将a初始化,全局变量,用于存储表中times中的综合
studentList=[] #创建studentList列表,用于抽取学号的表
insertOneStudent="insert into {}(student_no,score,date,times,checks) values(?,?,?,?,?)".format(dbTables["name"])
getStudentSql = "select score from {} where student_no=?".format(dbTables["name"])
getStudentTimesSql = "select times from {} where student_no=?".format(dbTables["name"])
getStudentchecksSql = "select student_no from {} where checks=1".format(dbTables["name"])
updateStudentScoreSql="update student set score=? where student_no=?"
updateStudentDateSql="update student set date=? where student_no=?"
updateStudentTimesSql="update student set times=? where student_no=?"
updateStudentChecksSql="update student set checks=? where student_no=?"
deleteStudentSql="delete from {} where student_no=?".format("student")
cur.execute(dbTables["createSql"]) #执行dbTables中的创建表的命令
#判断是否为空表
cur.execute(getStudentTimesSql,(2017249001,)) #查询表中学号为2017249001学号的times值
result=cur.fetchall()
if(result==[]): #若返回值为空,则说明表中没有数据,将会执行插入操作
for i in range(2017249001,2017249073):
cur.execute(insertOneStudent,(i,None,date,10,0))
cur.execute(deleteStudentSql,(2017249024,))
cur.execute(deleteStudentSql,(2017249038,))
cur.execute(deleteStudentSql,(2017249055,))
db.commit()
cur.close()
db.close()
db=sql.connect(dbPath)
cur=db.cursor()
#统计所有学生的times值总和
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
a=a+result[0][0]
#依次打印出每个学生被抽中的概率
print("下面是班级同学被抽到的概率表:")
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
c=(result[0][0])/a
print("学号为"+str(i)+"的学生被抽到的概率是%.2f"%c)
while(True):
date=datetime.datetime.now()
cur.execute(getStudentchecksSql) #得到表中学号对应checks值为1的值
studentCheck_equal1=cur.fetchall()
#print(studentCheck_equal1)
if(len(studentCheck_equal1)==1): #若studentCheck_equal1值为1 则执行此函数
studentCheck_equal1One=studentCheck_equal1[0][0]
def deleteStudent_no(n):
if(n!=studentCheck_equal1One):
return True
if(len(studentCheck_equal1)==2):
studentCheck_equal1One=studentCheck_equal1[0][0]
studentCheck_equal1two=studentCheck_equal1[1][0]
def deleteStudent_no(n):
if(n!=studentCheck_equal1One):
return True
if(n!=studentCheck_equal1two):
return True
print("是否要随机抽一名同学(是 or 否,若想退出程序则出入exit)",end=" ")
checkStudent=input()
while(checkStudent!="是" and checkStudent!="exit" and checkStudent!="否" ):
print("输入内容不规范,请重新输入(是 or 否,若想退出程序则出入exit):" , end=" ")
checkStudent=input()
if(checkStudent=="exit" ):
break
if(checkStudent=="是"): #若选择是,则将所有学号根据times次数依次加入至studentList中,并进行打乱
for i in range(2017249001,2017249073):
cur.execute(getStudentTimesSql,(i,))
result=cur.fetchall()
if(result==[]):
continue
for checkStudentNumber in range(0,result[0][0]):
studentList.append(i)
#print(studentList) 查看所有列表中的值
if(studentCheck_equal1!=[]): #将上次抽过的学生从列表中删除
studentList=filter(deleteStudent_no,studentList)
studentList=list(studentList)
#print(studentList)
random.shuffle(studentList)
print("抽到的学号为:"+str(studentList[0]))
print("请给此学号的成绩(0-100):",end=" ")
studentScore=input()
while(int(studentScore)<0 or (int(studentScore)>100)):
print("您输入的成绩不规范,请重新输入(0-100):",end=" ")
studentScore=input()
cur.execute(updateStudentScoreSql,(studentScore,studentList[0],))
cur.execute(updateStudentDateSql,(date,studentList[0],))
if(int(studentScore)>=60): #进行判定,若程序大于60分,则times值减一,若times值为1时,则不进行改变
cur.execute(getStudentTimesSql,(studentList[0],))
result=cur.fetchall()
if(result[0][0]==1):
break
times1=result[0][0]-1
cur.execute(updateStudentTimesSql,(times1,studentList[0],))
if(int(studentScore)<60): #进行判定,若程序大于60分,则times值加一
cur.execute(getStudentTimesSql,(studentList[0],))
result=cur.fetchall()
times1=result[0][0]+1
cur.execute(updateStudentTimesSql,(times1,studentList[0],))
print("是否需要再输入一个学生的成绩(是 or 否,若想退出程序则出入exit):",end=" ")
inputStudentNumberAgain=input()
while(inputStudentNumberAgain!="是" and inputStudentNumberAgain!="exit" and inputStudentNumberAgain!="否"):
print("输入内容不规范,请重新输入(是 or 否,若想退出程序则出入exit):" , end=" ")
inputStudentNumberAgain=input()
if(inputStudentNumberAgain=="是"):
print("请输入此学生的学号后两位:",end=" ")
studentNumber1=input()
while((int(studentNumber1)>73) or (int(studentNumber1)<0)): #若输入的值不符合规定,则需要重新输入
print("您输入的学号不规范,请重新输入(1-72):",end=" ")
studentNumber1=input()
while(studentNumber1=="24" or studentNumber1=="38" or studentNumber1=="55"):
print("班级里没有此学号的学生,请重新输入",end="")
studentNumber1=input()
studentNumber2=int(studentNumber1)+2017249000
print("请输入此学生的成绩(0-100):",end=" ")
studentScore1=input()
while((int(studentScore1)>100) or (int(studentScore1)<0)): #若输入的值不符合规定,则需要重新输入
print("您输入的成绩不规范,请重新输入(0-100):",end=" ")
studentScore1=input()
#加上checks修改函数
if(int(studentScore1)>=60):
cur.execute(getStudentTimesSql,(studentNumber2,))
result=cur.fetchall()
if(result[0][0]==1):
break
times1=result[0][0]-1
cur.execute(updateStudentTimesSql,(times1,studentNumber2,))
if(int(studentScore1)<60):
cur.execute(getStudentTimesSql,(studentNumber2,))
result=cur.fetchall()
times1=result[0][0]+1
cur.execute(updateStudentTimesSql,(times1,studentNumber2,))
cur.execute(updateStudentScoreSql,(studentScore1,studentNumber2,))
cur.execute(updateStudentDateSql,(date,studentNumber2,))
if(len(studentCheck_equal1)==1): #若上一次操作仅输入了一次,则执行此函数
cur.execute(updateStudentChecksSql,(0,studentCheck_equal1One,))
if(len(studentCheck_equal1)==2): #若上一次操作仅输入了两次,则执行此函数
cur.execute(updateStudentChecksSql,(0,studentCheck_equal1One,))
cur.execute(updateStudentChecksSql,(0,studentCheck_equal1two,))
if(checkStudent=="是"):
cur.execute(updateStudentChecksSql,(1,studentList[0],))
if(inputStudentNumberAgain=="是"):
cur.execute(updateStudentChecksSql,(1,studentNumber2,))
if(inputStudentNumberAgain=="exit"):
break
db.commit()
cur.close()
db.close()
总结
刚接触Python,写的水平还不是很高