在B站自學(xué)Python
站主:Python_子木
授課:楊淑娟
平臺(tái): 馬士兵教育
python: 3.9.9文章來源地址http://www.zghlxwxcb.cn/news/detail-421531.html
python打包exe文件
#安裝PyInstaller
pip install PyInstaller
#-F打包exe文件,stusystem\stusystem.py到py的路徑,可以是絕對(duì)路徑,可以是相對(duì)路徑
pyinstaller -F stusystem\stusystem.py
學(xué)生信息管理系統(tǒng)具體代碼如下
import os.path
filename='student.txt'
def main():
while True:
menu()
choice = int(input('請(qǐng)選擇'))
if choice in range(8):
if choice==0:
answer=input('您確定要退出系統(tǒng)嗎?y/n')
if answer.lower()=='y':
print('謝謝您的使用!')
break #退出系統(tǒng)
else:
continue
if choice==1:
insert()
if choice==2:
search()
if choice==3:
delete()
if choice==4:
modify()
if choice==5:
sort()
if choice==6:
total()
if choice==7:
show()
else:
print('無此功能,請(qǐng)按菜單重新選擇')
def menu():
print('=========================學(xué)生信息管理系統(tǒng)============================')
print('-----------------------------功能菜單------------------------------')
print('\t\t\t\t\t\t1.錄入學(xué)生信息')
print('\t\t\t\t\t\t2.查找學(xué)生信息')
print('\t\t\t\t\t\t3.刪除學(xué)生信息')
print('\t\t\t\t\t\t4.修改學(xué)生信息')
print('\t\t\t\t\t\t5.排序')
print('\t\t\t\t\t\t6.統(tǒng)計(jì)學(xué)生總?cè)藬?shù)')
print('\t\t\t\t\t\t7.顯示所有學(xué)生信息')
print('\t\t\t\t\t\t0.退出')
print('------------------------------------------------------------------')
def insert():
student_list=[]
while True:
id=input('請(qǐng)輸入ID(如1001):')
if not id:
break
name=input('請(qǐng)輸入姓名:')
if not name:
break
try:
english=int(input('請(qǐng)輸入英語成績(jī):'))
python=int(input('請(qǐng)輸入Python成績(jī):'))
java=int(input('請(qǐng)輸入Java成績(jī):'))
except:
print('輸入無效,不是整數(shù)類型,請(qǐng)重新輸入')
continue
#將錄入的學(xué)生保存到字典中
student={'id':id,'name':name,'english':english,'python':python,'java':java}
#將學(xué)生信息添加到列表中
student_list.append(student)
answer=input('是否繼續(xù)添加?y/n')
if answer.lower()=='y':
continue
else:
break
#調(diào)用save()函數(shù)
save(student_list)
print('學(xué)生信息錄入完畢!!!')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt=open(filename,'w',encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
def search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mode=input('按ID查找請(qǐng)輸入1,按姓名查找請(qǐng)輸入2:')
if mode=='1':
id=input('請(qǐng)輸入學(xué)生ID:')
elif mode=='2':
name=input('請(qǐng)輸入學(xué)生姓名:')
else:
print('您輸入有誤,請(qǐng)重新輸入')
continue
with open(filename,'r',encoding='utf-8') as rfile:
student=rfile.readlines()
for item in student:
d=dict(eval(item))
if id!='':
if d['id']==id:
student_query.append(d)
elif name!='':
if d['name']==name:
student_query.append(d)
#顯示查詢結(jié)果
show_student(student_query)
#清空列表
student_query.clear()
answer=input('是否要繼續(xù)查詢?y/n\n')
if answer.lower()=='y':
continue
else:
break
else:
print('暫未保存學(xué)生信息')
return
def show_student(lst):
if len(lst)==0:
print('沒有查詢到學(xué)生信息,無數(shù)據(jù)顯示!!!')
return
#定義標(biāo)題顯示格式
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
print(format_title.format('ID','姓名','英語成績(jī)','Python成績(jī)','Java成績(jī)','總成績(jī)'))
#定義內(nèi)容顯示格式
format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
int(item.get('english'))+int(item.get('python'))+int(item.get('java'))
))
def delete():
while True:
student_id=input('請(qǐng)輸入要?jiǎng)h除的學(xué)生的ID:')
if student_id!='':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as file:
student_old=file.readlines()
else:
student_old=[]
flag=False #刪除標(biāo)記
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={}
for item in student_old:
d=dict(eval(item)) #將字符串轉(zhuǎn)字典
if d['id']!=student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id為{student_id}的學(xué)生信息已被刪除')
else:
print(f'沒有找到ID為{student_id}的學(xué)生信息')
else:
print('無學(xué)生信息')
break
show() #刪除之后要重新顯示所有學(xué)生信息
answer=input('是否繼續(xù)刪除?y/n')
if answer.lower()=='y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_old=rfile.readlines()
else:
return
student_id=input('請(qǐng)輸入要修改的學(xué)員的ID:')
with open(filename,'w',encoding='utf-8') as wfile:
for item in student_old:
d=dict(eval(item))
if d['id']==student_id:
print('找到學(xué)生信息,可以修改他的相關(guān)信息了!')
while True:
try:
d['name']=input('請(qǐng)輸入姓名:')
d['english']=input('請(qǐng)輸入英語成績(jī):')
d['python']=input('請(qǐng)輸入python成績(jī):')
d['java']=input('請(qǐng)輸入java成績(jī):')
except:
print('您的輸入有誤,請(qǐng)重新輸入!!!')
else:
break
wfile.write(str(d)+'\n')
print('修改成功!')
show()
else:
print(f'未找到ID為{student_id}的學(xué)生信息')
wfile.write(str(d)+'\n')
answer=input('是否繼續(xù)修改其他學(xué)生信息?y/n\n')
if answer.lower()=='y':
modify()
def sort():
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
asc_or_desc=input('請(qǐng)選擇(0.升序,1.降序):')
if asc_or_desc=='0':
asc_or_desc_bool=False
elif asc_or_desc=='1':
asc_or_desc_bool=True
else:
print('您的輸入有誤,請(qǐng)重新輸入')
sort()
mode=input('請(qǐng)選擇排序方式(1.按英語成績(jī)排序 2.按Python成績(jī)排序 3.按Java成績(jī)排序 4.按總成績(jī)排序)')
if mode=='1':
student_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)
elif mode=='2':
student_new.sort(key=lambda x:int(x['python']),reverse=asc_or_desc_bool)
elif mode=='3':
student_new.sort(key=lambda x:int(x['java']),reverse=asc_or_desc_bool)
elif mode=='4':
student_new.sort(key=lambda x:int(x['english'])+int(x['python'])+int(x['java']),reverse=asc_or_desc_bool)
else:
print('您的輸入有誤,請(qǐng)重新輸入')
sort()
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)}名學(xué)生')
else:
print('還沒有錄入學(xué)生信息')
else:
print('暫未保存數(shù)據(jù)信息...')
def show():
student_list = []
if os.path.exists(filename):
# 定義標(biāo)題顯示格式
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}\t'
print(format_title.format('ID', '姓名', '英語成績(jī)', 'Python成績(jī)', 'Java成績(jī)', '總成績(jī)'))
with open(filename,'r',encoding='utf-8') as rfile:
student = rfile.readlines()
for item in student:
d = dict(eval(item))
student_list.append(d)
if len(student_list)==0:
format_nodata = '{:^6}'
print(format_nodata.format('無數(shù)據(jù)'))
# 定義內(nèi)容顯示格式
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
for item in student_list:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
int(item.get('english')) + int(item.get('python')) + int(item.get('java'))
))
student_list.clear()
else:
print('暫未保存過數(shù)據(jù)!!!')
if __name__ == '__main__':
main()
文章來源:http://www.zghlxwxcb.cn/news/detail-421531.html
到了這里,關(guān)于python學(xué)習(xí)-學(xué)生信息管理系統(tǒng)并打包exe的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!