Q1. Student Marks File – marks.
dat
Write a menu-driven program to accept Roll Number, Name and Marks of a student from the
user and store these details in a file called "[Link]" and display the contents of the file.
import pickle
def add():
f = open("[Link]", "ab")
rollno = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
data = [rollno, name, marks]
[Link](data, f)
[Link]()
print("Record added successfully")
def display():
try:
f = open("[Link]", "rb")
while True:
data = [Link](f)
print(data)
except EOFError:
[Link]()
while True:
print("\n1. Add Student Record")
print("2. Display Student Record")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add()
elif choice == 2:
display()
elif choice == 3:
print("Program Ended Successfully")
break
else:
print("Invalid Choice")
Q2. Employee Records – [Link]
Write a menu-driven program in Python that defines and calls the following user-defined
functions:
ADD() – To accept and add data of an employee to a CSV file "[Link]". Each record consists of
Employee ID, Employee Name and Employee Salary.
COUNT() – To count the number of records present in the CSV file "[Link]".
import csv
def add():
f = open("[Link]", "a", newline="")
w = [Link](f)
empid = int(input("Enter Employee ID: "))
name = input("Enter Employee Name: ")
salary = float(input("Enter Employee Salary: "))
data = [empid, name, salary]
[Link](data)
[Link]()
print("Record added successfully")
def count():
f = open("[Link]", "r", newline="")
r = [Link](f)
c=0
for row in r:
c=c+1
[Link]()
print("Number of Records =", c)
while True:
print("\n1. Add Employee Record")
print("2. Count Records")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add()
elif choice == 2:
count()
elif choice == 3:
print("Ending Program Successfully")
break
else:
print("Invalid Choice")
Q3. Employee Data – [Link]
This is based on the answer from your notebook. The missing question can be written as:
Write a menu-driven program in Python that defines and calls the following user-defined
functions:
ADD() – To accept and add data of an employee to a CSV file "[Link]". Each record consists of
Employee ID, Employee Name and Employee Salary.
SEARCH() – To search for an employee record based on the Employee ID entered by the user.
import csv
def add():
f = open("[Link]", "a", newline="")
w = [Link](f)
while True:
fid = int(input("Enter Employee ID: "))
fname = input("Enter Employee Name: ")
fsal = float(input("Enter Employee Salary: "))
data = [fid, fname, fsal]
[Link](data)
ch = input("Do you want to add another record? (Y/N): ")
if ch == "N" or ch == "n":
break
[Link]()
print("Record Added Successfully")
def search():
fid = int(input("Enter Employee ID to Search: "))
f = open("[Link]", "r", newline="")
r = [Link](f)
found = False
for row in r:
if int(row[0]) == fid:
print("Record Found")
print("Employee ID:", row[0])
print("Employee Name:", row[1])
print("Employee Salary:", row[2])
found = True
break
[Link]()
if found == False:
print("Record Not Found")
while True:
print("\n1. Add the Data")
print("2. Search the Data")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
add()
elif choice == 2:
search()
elif choice == 3:
print("Ending Program Successfully")
break
else:
print("Invalid Choice")