使用Python實現(xiàn)簡單的學生成績管理系統(tǒng)
基本功能:
- 能夠?qū)崿F(xiàn)學生成績相關信息的輸入、輸出、查找、刪除、修改等功能;(使用數(shù)據(jù)庫對數(shù)據(jù)進行存?。?/li>
- 輸入并存儲學生的信息:通過輸入學生的學號、姓名、和分數(shù),然后就可以把數(shù)據(jù)保存在建立的student文件里面。
- 打印學生的所有信息:通過一個打印函數(shù)就可以把所有的信息打印在屏幕上。
- 查找學生信息:這個功能通過輸入學號,查找該學生的信息,如果有該學號就輸出該學生的信息,沒有該學號就提示輸入的學號不存在。
- 刪除學生信息:該功能是對相應的學生進行刪除操作,如果學生存在就查找到進行刪除。
- 修改學生信息:這個功能首先通過查詢功能查詢出該學生是否存在,如果存在就對該學生的信息進行修改,如果不存在則返回到主界面。
實現(xiàn)效果:

制作技巧
1.定義變量
(1)定義交互輸入變量
主要用于展示學生成績管理系統(tǒng)的操作界面。
(2)定義學生成績信息變量
主要用于學生成績信息的存取。
s_info = """
*****************************************************
【學生成績管理系統(tǒng)】
q. 退出學生成績系統(tǒng)
1. 顯示學生成績信息
2. 新建學生成績信息
3. 查詢學生成績信息
4. 刪除學生成績信息
5. 修改學生成績信息
******************************************************"""
students=[]2.讀取學生成績信息
主要是從TXT文件中讀取學生成績信息,并保存到學生成績變量中。
f=open("students.txt","r+")
for st in f.readlines():
students.append(eval(st))
f.close()3.循環(huán)等待操作指令并執(zhí)行
主要是循環(huán)等待輸入的學生管理系統(tǒng)操作指令,獲取到操作指令后執(zhí)行對學生成績信息的顯示、新建、查詢、刪除、修改操作。
while True:
print(s_info)
handle = input('請選擇你要的操作選項:')
if handle == 'q':
print('q. 退出系統(tǒng)')
break
elif handle == '1':
s_display(students)
elif handle == '2':
s_new(students)
elif handle == '3':
s_find(students)
elif handle == '4':
s_delect(students)
elif handle == '5':
s_modify(students)
else:
print('請輸入正確的操作選項!')4.編寫操作指令執(zhí)行函數(shù)
(1)顯示學生成績信息函數(shù)
主要功能是顯示所有學生成績信息。
def s_display(students):
print('1. 顯示全部信息')
print('姓名\t語文\t數(shù)學\t英語\t總分')
for stu in students:
print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')(2)新建學生成績信息函數(shù)
主要是新建學生成績信息,并更新保存學生成績信息的文件。
def s_new(students):
print('2. 新建學生信息')
name = str(input('請輸入學生的姓名:'))
chinese = int(input('請輸入學生的語文成績:'))
math = int(input('請輸入學生的數(shù)學成績:'))
english = int(input('請輸入學生的英語成績:'))
total = chinese + math + english
stu = {'name': name, 'chinese': chinese, 'math': math, 'english': english, 'total': total}
students.append(stu)
s_write_to_file(students)(3)查詢學生成績信息函數(shù)
主要是查詢給定姓名的學生成績信息。
def s_find(students):
print('3. 查詢學生信息')
name = input('請輸入你要查詢學生的姓名:')
for stu in students:
if name == stu['name']:
print('姓名\t語文\t數(shù)學\t英語\t總分')
print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)(4)刪除學生成績信息函數(shù)
主要是刪除給定姓名的學生成績信息,并更新保存學生成績信息的文件。
def s_delect(students):
print('4. 刪除學生信息')
name = input('請輸入你要刪除學生的姓名:')
for stu in students:
if name == stu['name']:
students.remove(stu)
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)(5)修改學生成績信息函數(shù)
主要是修改給定姓名的學生成績信息,并更新保存學生成績信息的文件。
def s_modify(students):
print('5. 修改學生信息')
name = input('請輸入你要修改學生的姓名:')
for stu in students:
if name == stu['name']:
print('(如果不想修改,直接回車!)')
name = input('請重新輸入學生的姓名:')
chinese = input('請重新輸入學生的語文成績:')
math = input('請重新輸入學生的數(shù)學成績:')
english = input('請重新輸入學生的英語成績:')
if name:
stu['name'] = str(name)
if chinese:
stu['chinese'] = int(chinese)
if math:
stu['math'] = int(math)
if english:
stu['english'] = int(english)
stu['total'] = stu['chinese'] + stu['math'] + stu['english']
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)5.保存學生成績信息函數(shù)
主要實現(xiàn)學生成績信息的保存更新。
def s_write_to_file(students):
f=open("students.txt","r+")
for s in students:
f.write(str(s)+"\n")
f.close()完整源代碼
def s_write_to_file(students):
f=open("students.txt","r+")
for s in students:
f.write(str(s)+"\n")
f.close()
def s_display(students):
print('1. 顯示全部信息')
print('姓名\t語文\t數(shù)學\t英語\t總分')
for stu in students:
print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')
def s_new(students):
print('2. 新建學生信息')
name = str(input('請輸入學生的姓名:'))
chinese = int(input('請輸入學生的語文成績:'))
math = int(input('請輸入學生的數(shù)學成績:'))
english = int(input('請輸入學生的英語成績:'))
total = chinese + math + english
stu = {'name': name, 'chinese': chinese, 'math': math, 'english': english, 'total': total}
students.append(stu)
s_write_to_file(students)
def s_find(students):
print('3. 查詢學生信息')
name = input('請輸入你要查詢學生的姓名:')
for stu in students:
if name == stu['name']:
print('姓名\t語文\t數(shù)學\t英語\t總分')
print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)
def s_delect(students):
print('4. 刪除學生信息')
name = input('請輸入你要刪除學生的姓名:')
for stu in students:
if name == stu['name']:
students.remove(stu)
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)
def s_modify(students):
print('5. 修改學生信息')
name = input('請輸入你要修改學生的姓名:')
for stu in students:
if name == stu['name']:
print('(如果不想修改,直接回車!)')
name = input('請重新輸入學生的姓名:')
chinese = input('請重新輸入學生的語文成績:')
math = input('請重新輸入學生的數(shù)學成績:')
english = input('請重新輸入學生的英語成績:')
if name:
stu['name'] = str(name)
if chinese:
stu['chinese'] = int(chinese)
if math:
stu['math'] = int(math)
if english:
stu['english'] = int(english)
stu['total'] = stu['chinese'] + stu['math'] + stu['english']
break
else:
print('該學生不存在, 請檢查名字是否輸入正確!')
s_write_to_file(students)
if __name__=="__main__":
s_info = """
*****************************************************
【學生成績管理系統(tǒng)】
q. 退出學生成績系統(tǒng)
1. 顯示學生成績信息
2. 新建學生成績信息
3. 查詢學生成績信息
4. 刪除學生成績信息
5. 修改學生成績信息
******************************************************"""
students=[]
f=open("students.txt","r+")
for st in f.readlines():
students.append(eval(st))
f.close()
while True:
print(s_info)
handle = input('請選擇你要的操作選項:')
if handle == 'q':
print('q. 退出系統(tǒng)')
break
elif handle == '1':
s_display(students)
elif handle == '2':
s_new(students)
elif handle == '3':
s_find(students)
elif handle == '4':
s_delect(students)
elif handle == '5':
s_modify(students)
else:
print('請輸入正確的操作選項!')總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
在spyder IPython console中,運行代碼加入?yún)?shù)的實例
這篇文章主要介紹了在spyder IPython console中,運行代碼加入?yún)?shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python3爬蟲爬取英雄聯(lián)盟高清桌面壁紙功能示例【基于Scrapy框架】
這篇文章主要介紹了Python3爬蟲爬取英雄聯(lián)盟高清桌面壁紙功能,結(jié)合實例形式分析了基于Scrapy爬蟲框架進行圖片爬取的相關項目創(chuàng)建、文件結(jié)構(gòu)、功能實現(xiàn)操作技巧與注意事項,需要的朋友可以參考下2018-12-12
Pandas數(shù)據(jù)查詢的集中實現(xiàn)方法
本文主要介紹了Pandas數(shù)據(jù)查詢的集中實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
Python 運行.py文件和交互式運行代碼的區(qū)別詳解
這篇文章主要介紹了Python 運行.py文件和交互式運行代碼的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
selenium在執(zhí)行phantomjs的API并獲取執(zhí)行結(jié)果的方法
今天小編就為大家分享一篇selenium在執(zhí)行phantomjs的API并獲取執(zhí)行結(jié)果的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python神經(jīng)網(wǎng)絡Xception模型復現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡Xception模型復現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
Python爬蟲圖片懶加載技術 selenium和PhantomJS解析
這篇文章主要介紹了Python爬蟲圖片懶加載技術 selenium和PhantomJS解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
TensorFlow在MAC環(huán)境下的安裝及環(huán)境搭建
小編在論壇中看到很多朋友在尋找TensorFlow的環(huán)境搭建圖文步驟以及安裝的具體流程,在此小編給大家整理了一篇非常詳細的圖文流程,希望能夠幫助到你。2017-11-11
瀏覽器常用基本操作之python3+selenium4自動化測試(基礎篇3)
瀏覽器常用基本操作有很多種,今天給大家介紹python3+selenium4自動化測試的操作方法,是最最基礎的一篇,對python3 selenium4自動化測試相關知識感興趣的朋友一起看看吧2021-05-05

