10.
The file ‘Student’ contains the following information : Roll number, Name
and Marks
e. Append records to the file.
f. Search for a given roll number and display the name, if not found display
appropriate
message.
g. Input a roll number and update the marks.
PROGRAM
import pickle
fn='Student'
def display():
try:
with open(fn,'rb') as f:
print("ROLL NO","NAME","MARKS",sep='\t\t')
while True:
try:
e=[Link](f)
print(e[0],e[1],e[2],sep='\t\t')
except EOFError:
break
except FileNotFoundError:
print("No records in the file.")
def fileappend():
rno=input('Enter the roll number: ')
name=input('Enter the name of student: ')
while True:
try:
marks=int(input('Enter the marks: '))
break
except ValueError:
print("Invalid marks! Please enter a number.")
continue
data=[rno,name,marks]
with open(fn,'ab') as f:
[Link](data,f)
print('Record added successfully.')
def update_marks():
try:
disp=''
students=[]
with open(fn,'rb') as f:
while True:
try:
e=[Link](f)
[Link](e)
except EOFError:
break
search=input("Enter the student's roll number to updated marks: ")
for stud in students:
if stud[0]==search:
while True:
try:
new_marks=int(input('Enter the marks: '))
break
except ValueError:
print("Invalid marks! Please enter a number.")
continue
stud[2]=new_marks
with open(fn,'wb') as f:
for stud in students:
[Link](stud,f)
print("Marks updated successfully.")
with open(fn,'rb') as f:
while True:
try:
e=[Link](f)
print(e[0],e[1],e[2])
except EOFError:
break
disp=''
break
else:
disp='Student not found'
continue
print(disp)
except FileNotFoundError:
print("No records in the file.")
def display_name():
try:
with open(fn,'rb') as f:
search=input("Enter the student's roll number: ")
dis=''
while True:
try:
e=[Link](f)
if e[0]==search:
print('The name of the student is:')
dis=e[1]
break
else:
dis="Student not found."
except EOFError:
break
print(dis)
except FileNotFoundError:
print("No records in the file.")
while True:
display()
print("1. Append records to the file")
print("2. Search for a given roll no and display the name")
print("3. Update the marks of a student")
print("0. Exit")
try:
ch=int(input("Enter your choice: "))
except ValueError:
print("Invalid input. please enter a number.")
continue
if ch==1:
fileappend()
elif ch==2:
display_name()
elif ch==3:
update_marks()
elif ch==0:
print("Exited the program.")
break
else:
print("Invalid choice! Please try again.")
OUTPUT
ROLL NO NAME MARKS
1 Anjana 100
2 Avery 68
3 Chloe 100
1. Append records to the file
2. Search for a given roll no and display the name
3. Update the marks of a student
0. Exit
Enter your choice: 1
Enter the roll number: 4
Enter the name of student: Evan
Enter the marks: 89
Record added successfully.
ROLL NO NAME MARKS
1 Anjana 100
2 Avery 68
3 Chloe 100
4 Evan 89
1. Append records to the file
2. Search for a given roll no and display the name
3. Update the marks of a student
0. Exit
Enter your choice: 2
Enter the student's roll number: 2
The name of the student is:
Avery
ROLL NO NAME MARKS
1 Anjana 100
2 Avery 68
3 Chloe 100
4 Evan 89
1. Append records to the file
2. Search for a given roll no and display the name
3. Update the marks of a student
0. Exit
Enter your choice: 2
Enter the student's roll number: 7
Student not found.
ROLL NO NAME MARKS
1 Anjana 100
2 Avery 68
3 Chloe 100
4 Evan 89
1. Append records to the file
2. Search for a given roll no and display the name
3. Update the marks of a student
0. Exit
Enter your choice: 3
Enter the student's roll number to updated marks: 3
Enter the marks: 65
Marks updated successfully.
1 Anjana 100
2 Avery 68
3 Chloe 65
4 Evan 89
ROLL NO NAME MARKS
1 Anjana 100
2 Avery 68
3 Chloe 65
4 Evan 89
1. Append records to the file
2. Search for a given roll no and display the name
3. Update the marks of a student
0. Exit
Enter your choice: 0
Exited the program.