python實現學生管理系統,python學生管理系統

來源:互聯網
上載者:User

python實現學生管理系統,python學生管理系統

python寫的簡單的學生管理系統,練習python文法。

可以運行在windows和linux下,python 2.7。

#!/usr/local/bin/python # -*- coding:utf-8 -*-  import os import re  #定義學生類 class Student:  def __init__(self):   self.name = ''   self.ID = ''   self.score = 0  #根據學生分數進行從大到小的冒泡排序 def BuddleSortByScore( stulist ):  n = len( stulist )  for i in range( n ):   for j in range( n - i - 1):    if stulist[j].score < stulist[j+1].score:     #tmp = stulist[j+1]     #stulist[j+1] = stulist[j]     #stulist[j] = tmp     stulist[j],stulist[j+1] = stulist[j+1],stulist[j]  #按順序輸出所有學生的資訊 def PrintAllStudentInfo( stulist ):  print u"______________________學生列表_______________"  for i in range( len(stulist) ):   print u"姓名:" ,   print stulist[i].name,   print " " ,   print u"學號:" ,   print stulist[i].ID ,   print " " ,   print u"分數:" ,   print stulist[i].score  print "____________________________________________"  #增加一個學生,增加成功返回True,否則返回False def Add( stulist , stu ):  if searchByID( stulist , stu.ID ) == 1:   print u"此ID已經存在!"   return False  stulist.append( stu )   #給出是否儲存更新資料的選擇  print "Do you want to save the result ?"  nChoose = raw_input("Choose:Y/N:")    if nChoose == 'Y' or nChoose == 'y':   #將改變後的結果寫入檔案中,追加寫檔案   file_object = open("students.txt","a")   file_object.write( stu.ID )   file_object.write( " " )   file_object.write( stu.name )   file_object.write( " " )   file_object.write( str(stu.score) )   file_object.write( "\r\n" )   file_object.close()   return True  else:   stulist.remove(stu)  #根據ID刪除一個學生的資訊,刪除成功則返回True,否則返回false def DeleteByID( stulist , ID ):  for stu in stulist:   if stu.ID == ID:    stulist.remove( stu )     #選擇是否儲存已經改變的內容    print "Do you want to save the changed result ?"    nChoose = raw_input("Choose:Y/N:")        if nChoose == 'Y' or nChoose == 'y':     file_object = open("students.txt" , "w+")     for stu2 in stulist:      file_object.write(stu2.ID)      file_object.write(" ")      file_object.write(stu2.name)      file_object.write(" ")      file_object.write(str(stu2.score))      file_object.write("\r\n")     file_object.close()     print u"刪除成功!"    return True  print u"刪除失敗!"  return False   #根據姓名刪除一個學生的資訊,刪除成功返回True,否則返回False def DeleteByName( stulist , name ):  pos = searchByName( stulist , name )  if pos != -1:   del stulist[pos]    #選擇是否儲存已經改變的內容   print "Do you want to save the changed result ?"   nChoose = raw_input("Choose:Y/N:")       if nChoose == 'Y' or nChoose == 'y':     file_object = open("students.txt" , "w+")     for stu2 in stulist:      file_object.write(stu2.ID)      file_object.write(" ")      file_object.write(stu2.name)      file_object.write(" ")      file_object.write(str(stu2.score))      file_object.write("\r\n")     file_object.close()     print u"刪除成功!"   return True  else:   print u"刪除失敗!"   print pos   return False   #根據學號尋找一個學生是否存在,存在返回學生在列表中的下標,否則返回-1 def searchByID( stulist , ID ):  for i in range( len(stulist) ):   if stulist[i].ID == ID:    print u"姓名:" ,    print stulist[i].name ,    print " " ,    print u"學號:" ,    print stulist[i].ID ,    print " " ,    print u"分數:" ,    print stulist[i].score    return i  return -1  #根據姓名尋找一個學生是否存在,存在返回學生在列表中的下標,否則返回-1 def searchByName( stulist , name ):  for i in range( len(stulist) ):   if stulist[i].name == name:    print u"姓名:" ,    print stulist[i].name ,    print " " ,    print u"學號:" ,    print stulist[i].ID ,    print " " ,    print u"分數:" ,    print stulist[i].score    return i  return -1  #初始化,讀取檔案,更新學生資訊 def Init( stulist ):  print u"初始化......"    file_object = open("students.txt","r")   #按行讀取檔案中學生的資訊  for line in file_object:   stu = Student()   line = line.strip("\n")   s = line.split(" ")   stu.ID = s[0]   stu.name = s[1]   stu.score = s[2]   stulist.append(stu)  print u"初始化成功!"         #尋找菜單 def QueryMenu( stulist ):  while True:   print "******************************"   print u"根據學生的學號進行尋找-------1"   print u"根據學生的姓名進行尋找-------2"   print u"離開尋找模組----------------3"   print "******************************"    nChoose = raw_input("請輸入你的選擇")    if nChoose == "1":    ID = raw_input("請輸入你要輸入尋找的ID:")    searchByID( stulist , ID )   elif nChoose == "2":    name = raw_input("請輸入你要尋找的姓名:")    searchByName( stulist , name )   elif nChoose == "3":    return   else:    print u"選擇輸入錯誤,請重新輸入!"     #刪除模組 def DeleteMenu( stulist ):  while True:   print "*****************************"   print u"根據學生的學號進行刪除------1"   print u"根據學生的姓名進行刪除------2"   print u"離開刪除模組---------------3"   print "******************************"    nChoose = raw_input("請進行選擇:")    if nChoose == "1":    ID = raw_input("請輸入你要刪除的ID:")    DeleteByID( stulist , ID )   elif nChoose == "2":    name = raw_input("請輸入你要刪除的姓名:")    DeleteByName( stulist , name )   elif nChoose == "3":    return   else:    print u"您的選擇有誤,請重新輸入!"            #菜單 def menu( stulist ):  while True:   print "***********************"   print u"--------菜單------------"   print u"增加學生資訊---------1"   print u"尋找一個學生的資訊----2"   print u"刪除一個學生的資訊----3"   print u"輸出所有學生的資訊----4"   print u"根據分數排序---------5"   print u"退出程式-------------6"   print "------------------------"   print "************************"    nChoose = raw_input("請輸入你的選擇:")      if nChoose == "1":    stu = Student()    stu.name = raw_input("請輸入學生的姓名:")     #匹配學生ID是否滿足0--9中的數字    while True:     stu.ID = raw_input("請輸入學生的ID:")     #建立編譯一個Regex的模板     p = re.compile( '^[0-9]{3}$' )     if p.match( stu.ID ):      break     else:      print "學生的ID輸入錯誤,正確形式應該為0--9之間的三位元字!"     #匹配學生的分數是否滿足0--100之間    while True:      stu.score = eval( raw_input("請輸入學生的分數:") )     #利用普通的符號來判斷分數是否符合標準     #if stu.score >= 0 and stu.score <= 100:     # break     #利用Regex來判斷分數是否符合標準     if re.match( "^[0-9]" ,str(stu.score) ) and stu.score<=100 and    stu.score >= 0 :      break     else:      print u"分數不滿足實際情況,應該為0--100之間的數字!"     if Add( stulist , stu ) == 1:     print u"學生資訊增加成功!"    else:     print u"學生資訊增加失敗!"   elif nChoose == "2":    QueryMenu( stulist )   elif nChoose == "3":    DeleteMenu( stulist )   elif nChoose == "4":    PrintAllStudentInfo( stulist )   elif nChoose == "5":    BuddleSortByScore( stulist )     print "Do you want to save the sorted result?"    choose = raw_input("please input your choice:Y/N:")    if choose == 'Y' or choose == 'y':     file_object = open("students.txt","w+")     for stu2 in stulist:      file_object.write(stu2.ID)      file_object.write(" ")      file_object.write(stu2.name)      file_object.write(" ")      file_object.write(str(stu2.score))      file_object.write("\r\n")   elif nChoose == "6":    return   else:    print u"輸入有誤,請重新輸入!"     #測試函數部分 if __name__ == '__main__':  #定義一個列表用來儲存所有學生的資訊  stulist = []    #初始化,從檔案中讀取資訊  Init( stulist )    #菜單函數  menu( stulist ) 

運行需要讀寫檔案Students.txt。檔案格式如:

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.