0% found this document useful (0 votes)
5 views2 pages

P 5

The document is a Python script for managing student records using pickle for data serialization. It includes functions to load, save, add, search, and display student records based on roll numbers. A menu-driven interface allows users to interact with the system to perform these operations.

Uploaded by

nitikapradhan077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

P 5

The document is a Python script for managing student records using pickle for data serialization. It includes functions to load, save, add, search, and display student records based on roll numbers. A menu-driven interface allows users to interact with the system to perform these operations.

Uploaded by

nitikapradhan077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pickle

import os

filename = "students.dat"

def load_records():
if os.path.exists(filename):
with open(filename, "rb") as file:
return pickle.load(file)
else:
return []

def save_records(records):
with open(filename, "wb") as file:
pickle.dump(records, file)

def add_record():
records = load_records()
rollno = input("Enter roll number: ")
# Check if rollno already exists
for rec in records:
if rec['rollno'] == rollno:
print("Record with this roll number already exists!")
return
name = input("Enter name: ")
try:
marks = float(input("Enter marks: "))
except ValueError:
print("Invalid marks. Must be a number.")
return
record = {'rollno': rollno, 'name': name, 'marks': marks}
records.append(record)
save_records(records)
print("Record added successfully.")

def search_record():
records = load_records()
rollno = input("Enter roll number to search: ")
for rec in records:
if rec['rollno'] == rollno:
print(f"Record Found: Roll No: {rec['rollno']}, Name: {rec['name']},
Marks: {rec['marks']}")
return
print("Record not found.")

def display_all():
records = load_records()
if not records:
print("No records found.")
return
print("All Student Records:")
for rec in records:
print(f"Roll No: {rec['rollno']}, Name: {rec['name']}, Marks:
{rec['marks']}")

def menu():
while True:
print("\n--- Student Records Menu ---")
print("1. Add new record")
print("2. Search student by roll number")
print("3. Display all records")
print("4. Exit")
choice = input("Enter your choice (1-4): ")

if choice == '1':
add_record()
elif choice == '2':
search_record()
elif choice == '3':
display_all()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

menu()

You might also like