Write a menu driven program in Python that asks the user to add, display, and search records of students stored in a binary file.

The student record contains roll no, name and test score. It should be stored in a dictionary object.

Your program should pickle the object and save it to a binary file.

 

import pickle

def set_data():
    rollno = int(input('Enter roll number: '))
    name = input('Enter name: ')
    test_score = int(input('Enter test score: '))
    print()
    
    #create a dictionary
    student = { }
    student['rollno'] = rollno
    student['name'] = name
    student['test_score'] = test_score
    
    return student

def display_data(student):
    print('Roll number:', student['rollno'])
    print('Name:', student['name'])
    print('Test Score:', student['test_score'])
    print()


def write_record():
    #open file in binary mode for writing.
    outfile = open('student.dat', 'ab')

    #serialize the object and writing to file
    pickle.dump(set_data(), outfile)

    #close the file
    outfile.close()


def read_records():
    #open file in binary mode for reading
    infile = open('student.dat', 'rb')

    #read to the end of file.
    while True:
        try:
            #reading the oject from file
            student = pickle.load(infile)

            #display the object
            display_data(student)
        except EOFError:
            break

    #close the file
    infile.close()

def search_record():
    infile = open('student.dat', 'rb')
    rollno = int(input('Enter rollno to search: '))
    flag = False
    
    #read to the end of file.
    while True:
        try:
            #reading the oject from file
            student = pickle.load(infile)

            #display record if found and set flag
            if student['rollno'] == rollno:
                display_data(student)
                flag = True
                break
            
        except EOFError:
            break

    if flag == False:
        print('Record not Found')
        print()
        
    #close the file
    infile.close()

def show_choices():
    print('Menu')
    print('1. Add Record')
    print('2. Display Records')
    print('3. Search a Record')
    print('4. Exit')

def main():
    while(True):
        show_choices()
        choice = input('Enter choice(1-4): ')
        print()
        
        if choice == '1':
            write_record()
            
        elif choice == '2':
            read_records()

        elif choice == '3':
            search_record()

        elif choice == '4':
            break
        
        else:
            print('Invalid input')
            
#call the main function.
main()

 

Output:

Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): "1"
()
Enter roll number: 1
Enter name: "Amit"
Enter test score: 23
()
Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): "2"
()
('Roll number:', 1)
('Name:', 'Amit')
('Test Score:', 23)
()
Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4): "3"
()
Enter rollno to search: 1
('Roll number:', 1)
('Name:', 'Amit')
('Test Score:', 23)
()
Menu
1. Add Record
2. Display Records
3. Search a Record
4. Exit
Enter choice(1-4):

Screenshots-

program in Python that asks the user to add display and search records

 

menu driven program2

By cbsepython

A complete solution for the students of class 9 to 12 having subject Information Technology (402), Computer Science (083). Explore our website for all the useful content as Topic wise notes, Solved QNA, MCQs, Projects and Quiz related to the latest syllabus.