新手编程第一个程序,历经一个月的完善,比其他都完整,保存一下。
重要知识点:
①json是用来输出一个列表,又读取一个列表的
----------输出一个列表到文件中,又在运行中读取这个文件,这样就可以反复运行啦
(不怕重复数据,因为我多加了重复就不允许的)
具体用法:https://www.cnblogs.com/xiaomingzaixian/p/7286793.html
②如果弄了main()这条式子,你用return时就会返回到这个主程序里
if __name__=='__main__': #以主程序形式运行 有这条就可以利用return,返回主程序
main()
③格式化字符串,f不可或缺,不然就输出不了对应的数据了
print(f'ID为{student_id}的学生信息已修改')
④程序里的注释都是知识点!
⑤基础知识学习指路
https://www.bilibili.com/video/BV1wD4y1o7AS
⑥其余基础知识笔记
网盘待补充
# 希望不久的未来能与更好的自己碰面
import os.path
import json
filename='student.txt' #文件名
try:
file = open('1.json','r',encoding='utf-8')#
id1 = json.load(file)#读取json信息
except:
file = open('1.json', 'w', encoding='utf-8') #
try:
id1 = json.load(file) # 读取json信息
except:
id1=[]
try:
file1 = open('2.json','r',encoding='utf-8')#
name1 = json.load(file1)#读取json信息
except:
file1 = open('2.json', 'w', encoding='utf-8') #
try:
name1 = json.load(file) # 读取json信息
except:
name1=[]
def main():
while True:
menu()
try:
choice=int(input('请选择:'))
except:
print('输入格式错误,请重新输入')
continue
if choice in range(0,8):
if choice==0:
answer=input('您确定要退出系统吗?y/n\n')
if answer=='y' or answer=='Y':
json_info = id1
file = open('1.json', 'w', encoding='utf-8')
json.dump(json_info, file) # 将json写入文件
json_info1 = name1
file1 = open('2.json', 'w', encoding='utf-8')
json.dump(json_info1,file1) # 将json写入文件
print('谢谢您的使用!')
break #结束循环,退出系统
else:
continue
elif choice==1:
insert()#录入学生信息
elif choice ==2:
search()#查找学生信息
elif choice ==3:
delete()#删除学生信息
elif choice==4:
modify()#修改学生信息
elif choice==5:
sort()#排序
elif choice==6:
total()#统计学生总人数
elif choice==7:
show()#显示所有学生信息
def menu():
print(''' ----------------------------学生信息管理系统------------------
_____________________________功能菜单------------------------
1.录入学生信息
2.查找学生信息
3.删除学生信息
4.修改学生信息
5.排序
6.统计学生总人数
7.显示所有学生信息
0.退出系统
-----------------------------------------------------------
''')
def insert():
student_list = [] # 用于inseret
insert1=True
while insert1==True:
id=input('请输入ID(比如1001:')
if not id:
print('输入错误')
continue
if id in id1:
print('ID已存在,请重新输入')
continue
id1.append(id)
while insert1==True: #这个缩进关系表示当姓名重复时,只重新输入姓名即可
name=input('请输入姓名:')
if not name :
print('输入错误')
continue
if name in name1:
print('账号已存在,请重新输入')
continue
name1.append(name)
break#跳出姓名的循环,继续id这个大循环
while insert1==True:
try:
english=int(input('请输入英语成绩:'))
except:
print('输入无效,不是整数类型,请重新输入')
continue
while insert1==True:
try:
chinese=int(input('请输入语文成绩:'))
except:
print('输入无效,不是整数类型,请重新输入')
continue
while insert1==True:
try:
mathematics=int(input('请输入数学成绩:'))
insert1=False
except:
print('输入无效,不是整数类型,请重新输入')
continue
#将录入的学生信息保存到字典中
student={'id':id,'name':name,'english':english,'chinese':chinese,'mathematics':mathematics}
#将学生信息添加到列表中
student_list.append(student)
save(student_list)
answer=input('是否继续添加?y/n\n')
if answer=='y':
insert()
else:
print('学生信息录入完毕!')
break
def save(lst):
try:
stu_str=open(filename,'a',encoding='utf-8')#怕中文乱码加encoding
except:
stu_str = open(filename, 'w', encoding='utf-8') #没有就创建
for item in lst:#遍历列表,因为可能一次性输入很多学生
stu_str.write(str(item)+'\n')#write要用+号连接两个str,否则就相当于用两个write,这是不允许的
stu_str.close()
def search():
while True:
student_query = [] # 相当于clear,放在循环里就不用clear了
if os.path.exists(filename):
id=''
name='' #先设为空
mode=input('按ID查找请输入1,按姓名查找请输入2\n')
if mode=='1':
id=input('请输入学生ID:')
elif mode=='2':
name=input('请输入学生姓名:')
else:
print('您的输入有误,请重新输入')
continue
with open(filename,'r',encoding='utf-8') as rfile:
student=rfile.readlines() #student读取全部信息
for item in student:#遍历student
d = dict(eval(item))
if id!='':
if d['id'] == id:
student_query.append(d)
if name!=' ':#以姓名查找
if d['name']==name: #找到就加元素,找不到的在show里会统一输出查询不到
student_query.append(d)
#显示查询结果
show_student(student_query)
#请空列表
answer=input('请问是否继续查询?y/n\n')
if answer=='y':
continue #while循环里有student_query=[] 相当于清空列表了
else:
print('查询结束。')
return
else:
print('暂未保存学生信息,请先录入学生信息')
return#返回主程序
def show_student(lst):
if len(lst)==0: #不满足前面if条件,所以还是空值,即长度为0,所以输出查询不到
print('没有查询到学生信息,无数据显示!')
return
#定义标题显示格式
format_title='{:^6}\t{:^8}\t{:^10}\t{:^10}\t{:^10}\t{:^10}'
print(format_title.format('ID','姓名','英语成绩','语文成绩','数学成绩','总成绩'))
#定义内容的显示格式
format_data='{:^6}\t{:^8}\t{:^12}\t{:^12}\t{:^12}\t{:^12}'
for item in lst:#遍历字典,把所有字典都读一遍,从而输出全部
print(format_data.format(item.get('id'),item.get('name'),item.get('english'),item.get('chinese'),item.get('mathematics'),int(item.get('english'))+int(item.get('chinese'))+int(item.get('mathematics'))))
def delete():
while True:
student_id=input('请输入需要删除的学生的id:')
if student_id!=' ':#id不为空(正常输入就有下一步,不然就重复)
if os.path.exists(filename):#判断filename文件会否存在,存在就进行下面
with open(filename,'r',encoding='utf-8') as file: #filename记做file with open形式可以不用谢close(),自动关闭
student_old=file.readlines()#读取多行学生信息,返回一个列表
else:
student_old=[] #文件里没有学生信息
#第一个if是为了引出student_old列表里的学生信息
flag=False #标记是否删除
if student_old:#有学生信息的话
with open(filename,'w',encoding='utf-8') as wfile:#重写文件记做wfile
d={}#空字典
for item in student_old:#遍历列表 用于是否有相同的id
d=dict(eval(item))#eval输出字符串,内置函数dict创建字典,将student_old存入字典d
if d['id']!=student_id: #如果输入的id不相同,则写入wfile
wfile.write(str(d)+'\n')
else:#有相同id 的话不写入
d=eval(item) #字符串转字典
c = list(d.values()) #字典取value值
cc = c[0]
id1.remove(cc)
# delete_id(cc)
ccc=c[1]
name1.remove(ccc)
# delete_name(ccc)
flag=True #标记已删除
if flag:
print(f'id为{student_id}的学生信息已被删除')#有f才行
else:
print(f'没有找到ID为{student_id}的学生信息')
else:
print('无学生信息') #是完全没有
show() #删除之后要重新显示所有学生信息
answer=input('是否继续删除学生信息?y/n')
if answer=='y':
continue
else:
print('删除操作已完成')
break
def modify():
while True:
student_id=input('请输入需要修改的学生的id:')
if student_id!=' ':#id不为空(正常输入就有下一步,不然就重复)
if os.path.exists(filename):#判断filename文件会否存在,存在就进行下面
with open(filename,'r',encoding='utf-8') as rfile: #filename记做mfile with open形式可以不用写close(),自动关闭
student_old=rfile.readlines()#读取多行学生信息,返回一个列表
else:
print('无学生信息,请先录入学生信息')
student_old=[] #文件里没有学生信息
return
#第一个if是为了引出student_old列表里的学生信息
flag=0
if student_old:#有学生信息的话
with open(filename,'w+',encoding='utf-8') as mfile:#重写文件记做mfile
d={}#空字典
for item in student_old:#遍历列表 用于是否有相同的id
d=eval(item)#eval输出字符串,内置函数dict创建字典,将student_old存入字典d
if d['id'] == student_id:
print('找到学生信息,可以修改他的相关信息了!')
aaa = list(d.values())
aaaa = aaa[1]
name1.remove(aaaa)
# delete_namae(aaaa) # 去掉遍历到的数据,把修改前的数据去掉,后面再增加新数据
while True: # 上面表示每一次都要全部遍历一遍,把文件重写(每一次)
try:
d['name'] = input('请输入姓名:')
if d['name'] in name1:
print('name已存在,请重新输入')
continue
d['english'] = int(input('请输入英语成绩:'))
d['chinese'] = int(input('请输入语文成绩:'))
d['mathematics'] = int(input('请输入数学成绩:'))
print(f'ID为{student_id}的学生信息已修改') # 有f才行
break
except:
print('您的输入有误,请重新输入!!')
continue
a = list(d.values())
bbbb = a[1]
name1.append(bbbb)
#以上5行用于将修改后的数据也放入列表,这样insert时就不会重复了
flag=1
mfile.write(str(d) + '\n')
if flag!=1:#全部都没有经过if的才会输出这个
print(f'没有找到ID为{student_id}的学生信息')
print('现所有信息如下:')
show() # 修改之后要重新显示所有学生信息
answer = input('是否继续修改学生信息?y/n')
if answer == 'y':
continue
else:
print('修改完成')
return
else:
print('输入格式错误,请重新输入')
continue
'''def modify():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:#重写文件记做rfile
student_old=rfile.readlines() #读取fliename的多行信息,放在student_old中
else:
return #没有文件就返回菜单
with open(filename,'w+',encoding='utf-8') as wfile:#可读写,重写文件
while True:
try:
student_id = int(input('请输入需要修改学生信息的ID:'))
break
except:
print('输入非法符号,请重新输入')
continue
for item in student_old: # item遍历student_old
d = dict(eval(item)) # 将item存入字典d
if d['id'] == student_id: # 根据键获取值
print('找到学生信息,可以修改他的相关信息了!')
while True: # 上面表示每一次都要全部遍历一遍,把文件重写(每一次)
try:
d['name'] = input('请输入姓名:')
d['english'] = int(input('请输入英语成绩:'))
d['chinese'] = int(input('请输入语文成绩:'))
d['mathematics'] = int(input('请输入数学成绩:'))
wfile.write(str(d) + '\n') # 改好就继写入wfile
print('修改成功!!')
break
except:
print('您的输入有误,请重新输入!!')
continue
else:
wfile.write(str(d) + '\n') # 遍历但是不等于输入的id 就直接写入wfile,不作任何修改
# for 循环,将wfile循环填满
for i in wfile:
ii=dict(eval(i)) #将i存入字典ii
for key,value in ii.items():
aa.append(value)
if student_id not in aa:#判断元素是否在列表内
print('没有找到该名学生信息')
answer=input('是否继续修改其他学生信息呢?y/n\n')
if answer=='y':
modify() #也是循环的一种方法,不是y直接结束,是y就又重头运行
else:
print('修改完毕!')
'''
def sort():
show()# 在show里已经判断了文件是否存在了,但是没有退出,还会运行下面操作,所以要再次判断,做选择
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_list=rfile.readlines()
student_new=[]
for item in student_list:
d=dict(eval(item))
student_new.append(d)#遍历成字典存入列表中
else:
return
while True:
asc_or_desc=input('请选择(0.升序 1.降序):')
if asc_or_desc=='0':
asc_or_desc_bool=False #升序
break
elif asc_or_desc == '1':
asc_or_desc_bool = True #降序.
break
else:
print('您的输入有误,请重新输入')
continue
while True:
mode = input('请选择排序方式(1.按英语成绩排序 2.按语文成绩排序 3.按数学成绩排序 4.按总成绩排序):')
if mode=='1':
student_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)#匿名函数lambda
break
elif mode =='2':
student_new.sort(key=lambda x:int(x['chinese']),reverse=asc_or_desc_bool)
break
elif mode == '3':
student_new.sort(key=lambda x:int(x['mathematics']),reverse=asc_or_desc_bool)
break
elif mode == '4':
student_new.sort(key=lambda x:int(x['english'])+int(x['chinese'])+int(x['mathematics']),reverse=asc_or_desc_bool)
break
else:
print('您的输入有误,请重新输入')
continue
show_student(student_new)
def total():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
if students:
print(f'一共有{len(students)}名学生')
else:
print('还没有录入学生信息')
else:
print('暂为保存任何数据信息……')
def show():
student_list=[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
for item in students:
student_list.append(eval(item)) #把数据添加到列表 item变为字典
if student_list:
show_student(student_list)
else:
print('暂未保存书序信息!')#在别的函数里进行完这个判断,还会继续下面操作
if __name__=='__main__': #以主程序形式运行 有这条就可以利用return,返回主程序
main()
运行页面
相关资源:https://download.csdn.net/download/lue_dong/20678411?spm=1001.2014.3001.5503