~ How to create a Search feature, Search by ID, Python


Sample Project - Form Tutor Management System

*Teachers with subscriptions will have access to all worked solutions and python code. This sample project is based on OCR GCSE NEA Task 1

CHALLENGE: 

Searching is important when you're making a system that stores details of any kind. In this task you need to create a SEARCH BY ID feature. The form tutor or use should be able to put in an ID number, and the student record (i.e. all the details for that student) should be displayed on the screen

SUGGESTED SOLUTION / CODE: 

Note: See if you can do better (and you should be able to). The solutions provided are basic, and allow for discussion on possible alternative methods and solutions.

#Form Tutor Management System
import csv
import sys
def main():
    login()
    
def login():
    #declare variables. Enter data for username and password
    username="qwe"
    password="qwe123"
    #Prompt the user to enter their username and password
    print("Enter username : ")
    answer1=input()
    print("Enter password : ")
    answer2=input()
    #if the responses for username and password equal the declarations, access granted
    if answer1==username and answer2==password:
        print("Welcome - Access Granted")
        menu()

def menu():
    print("************MAIN MENU**************")
    #time.sleep(1)
    print()
    #print all the menu options:
    choice = input("""
                      A: Enter Student details
                      B: View Student details
                      C: Search by ID number
                      D: Produce Reports
                      Q: Quit/Log Out

                      Please enter your choice: """)
    #if the choice is A,B,C, D or Q, go to the corresponding subroutine
    if choice == "A" or choice =="a":
        enterstudentdetails()
    elif choice == "B" or choice =="b":
        viewstudentdetails()
    elif choice == "C" or choice =="c":
        searchbyid()
    elif choice=="D" or choice=="d":
        producereports()
    elif choice=="Q" or choice=="q":
        sys.exit
    else:
        #error checking - if they put in nonsense, make sure they know what to do!
        print("You must only select either A,B,C, or D.")
        print("Please try again")
        #Return to main menu for further options or to quit the program
        menu()

def enterstudentdetails():
    #user is prompted to input all the required fields
    print("Enter id")
    id=input()
    print("Enter first name")
    firstname=input()
    print("Enter surname")
    surname=input()
    print("Enter Date of Birth Format: dd/mm/yy")
    dob=input()
    print("Enter first line of address")
    firstlineaddress=input()
    print("Enter Postcode")
    postcode=input()
    print("Enter Gender")
    gender=input()
    print("Enter Tutor Group")
    tutorgroup=input()
    print("Enter email address")
    email=input()
    #open the file
    with open('studentfile.txt','a') as studentfile:
        #create a studentfile Writer - enables us to perform 'write' operations
        studentfileWriter=csv.writer(studentfile)
        #use the studenfile Writer to write all the variables entered to the file (as as a row)
        studentfileWriter.writerow([id,firstname,surname,dob,firstlineaddress,postcode,gender,tutorgroup,email])
        print("Record has been written to file")
        #Return to main menu for further options or to quit the program
        menu()

def viewstudentdetails():
#Open the file for reading
    f=open("studentfile.txt","r",encoding="utf8")
    #Create a list called "displaylist" into which all the files lines are read in to....
    displaylist=f.read()
    #print the list (that now has the file details in it)
    print(displaylist)
    f.close()
    menu()

def searchbyid():
    #open the file as student file (variable)
    with open("studentfile.txt","r") as studentfile:
        #prompt the user to enter the ID number they require
        idnumber=input("Enter the ID number you require:")
        #call upon our reader (this allows us to work with our file)
        studentfileReader=csv.reader(studentfile)
        #for each row that is read by the Reader    
        for row in studentfileReader:
            #and for each field in that row (this does it automatically for us)
            for field in row:
                #if the field is equal to the id number that is being searched for
                if field ==idnumber:
                    #print the row corresponding to that ID number
                    print(row)
                    #Return to main menu for further options or to quit the program
        menu()
                
def producereports():
    pass
    #Teacher can produce clever reports such as:
    #a) list of names of males and email addresses (to email a reminder about boys football club)
    #b) list of names of females in specific postcode (to remind them of a girls coding club in the area)
    #c) list of all names, birthdays and addresses (to send out birthday cards!)
    
#the program is initiated, so to speak, here
main()

Analyse

Write your own summary of the problem. What are your objectives? List the success criteria

Design Tools

Designing something or writing out some pseudocode before you actually write code is always a good idea! Get in to the habit of doing so! You can draw your flowchart here and screenshot it.

Try it yourself

Testing Table

You may want to read a little about Testing first. A teacher may go through some examples with you. Feel free to fill in the test table here, and screenshot it in to your powerpoint. Testing is absolutely essential once you have created a program!
Test No. Description Test Data(input) Expected Outcome Actual Outcome Further Action?
1
2
3
4
5
Coming soon!