Python implements the student management system and python student management system

Source: Internet
Author: User

Python implements the student management system and python student management system

A simple student management system written in python to practice the python syntax.

You can run python 2.7 on windows and linux.

#! /Usr/local/bin/python #-*-coding: UTF-8-*-import OS import re # define Student class Student: def _ init _ (self): self. name = ''self. ID = ''self. score = 0 # Sort def BuddleSortByScore (stulist): n = len (stulist) for I in range (n) based on the students' scores in a bubble sequence ): 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] # outputs information of all students in sequence def PrintAllStudentInfo (stulist ): print u "______________________ student list _______________" for I in range (len (stulist): print u "name:", print stulist [I]. name, print "", print u "student ID:", print stulist [I]. ID, print "", print u "score:", print stulist [I]. score print "________________________________________________" # Add a student and return True if the student is added successfully. Otherwise, False def Add (stulist, st is returned. U): if searchByID (stulist, stu. ID) = 1: print u "this ID already exists! "Return False stulist. append (stu) # specifies whether to save the updated data. print" Do you want to save the result? "NChoose = raw_input (" Choose: Y/N: ") if nChoose = 'y' or nChoose = 'y': # Write the changed result to the file, append Write File 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) # Delete the student information based on the ID. If the student information is deleted successfully, True is returned. Otherwise, false d is returned. Ef DeleteByID (stulist, ID): for stu in stulist: if stu. ID = ID: stulist. remove (stu) # select whether to save the changed content 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 "deleted successfully! "Return True print u" failed to delete! "Return False # True is returned if a student is deleted successfully based on the name. Otherwise, False def DeleteByName (stulist, name): pos = searchByName (stulist, name) if pos! is returned! =-1: del stulist [pos] # select whether to save the changed content 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 "deleted successfully! "Return True else: print u" failed to delete! "Print pos return False # check whether a student exists based on the student ID. If a student has a subscript in the list,-1 def searchByID (stulist, ID) is returned ): for I in range (len (stulist): if stulist [I]. ID = ID: print u "name:", print stulist [I]. name, print "", print u "student ID:", print stulist [I]. ID, print "", print u "score:", print stulist [I]. score return I return-1 # check whether a student exists according to the name, and return the subscript of the student in the list; otherwise, return-1 def searchByName (stulist, name ): for I in range (len (s Tulist): if stulist [I]. name = name: print u "name:", print stulist [I]. name, print "", print u "student ID:", print stulist [I]. ID, print "", print u "score:", print stulist [I]. score return I return-1 # initialization, reading files, updating student information def Init (stulist): print u "initialization ...... "file_object = open (" students.txt "," r ") # Read the information of Middle School Students in a file by row 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 "initialization successful! "# Query menu def QueryMenu (stulist): while True: print "*****************************" print u "based on the student's student ID search ------- 1 "print u" by Student name search ------- 2 "print u" Left search module ---------------- 3 "print "************ * ***************** "nChoose = raw_input (" Enter your choice ") if nChoose = "1": ID = raw_input ("Enter the ID you want to search for:") searchByID (stulist, ID) elif nChoose = "2 ": name = raw_input ("Enter the name you want to search for:") searchByName (stulist, Name) elif nChoose = "3": return else: print u "select an incorrect input. Please enter it again! "# Delete module def DeleteMenu (stulist): while True: print "***************************" print u "based on the student's student ID delete ------ 1 "print u" delete based on student name ------ 2 "print u" Remove module ------------- 3 "print "************* * **************** "nChoose = raw_input (" Please select: ") if nChoose =" 1 ": ID = raw_input (" Enter the ID you want to delete: ") DeleteByID (stulist, ID) elif nChoose =" 2 ": name = raw_input ("Enter the name you want to delete:") DeleteByName (stulist, name) Elif nChoose = "3": return else: print u "Your selection is incorrect. Please enter it again! "# Menu def menu (stulist): while True: print "**********************" print u "-------- menu ------------" print u "add student information --------- 1 "print u" search for information about a student ---- 2 "print u" delete information about a student ---- 3 "print u" output information about all students ---- 4 "print u" based on scores sort --------- 5 "print u" Exit program ------------- 6 "print" ---------------------- "print "********************** ** "nChoose = raw_input (" Enter your choice: ") if nChoose =" 1 ": stu = Student () stu. name = ra W_input ("Enter the Student name:") # match whether the student ID meets the number 0-9 while True: stu. ID = raw_input ("Enter student ID:") # create a template for compiling a regular expression p = re. compile ('^ [0-9] {3} $') if p. match (stu. ID): break else: print "student ID input error. The correct format should be three digits between 0 and 9! "# Match whether the score of a student is between 0--100 while True: stu. score = eval (raw_input ("Enter the student's score:") # Use common symbols to determine if the score meets the criteria # if stu. score> = 0 and stu. score <= 100: # break # use a regular expression to determine whether the score meets the standard if re. match ("^ [0-9]", str (stu. score) and stu. score <= 100 and stu. score> = 0: break else: print u "score does not meet the actual situation, it should be a number between 0 and! "If Add (stulist, stu) = 1: print u" student information added successfully! "Else: print u" failed to add student information! "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 "is incorrect. Please enter it again! "# Test function section if _ name _ = '_ main _': # define a list to store information of all students. stulist = [] # initialization, read Information from the file Init (stulist) # menu function menu (stulist)

Run the dig command to read and write the students.txt file. The file format is as follows:

For more information, see management system development.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.