4.binary Create and Search
4.binary Create and Search
Search for a given roll number and display the name, if not found display
appropriate message.
import pickle
def create():
f=open(r'G:\Juliet 2023-2024\students.dat','wb')
rec=[]
while True:
rno=int(input("Enter roll no : "))
name=input('Enter name : ')
data=[rno,name]
rec.append(data)
ch=input('Do you want to continue (y/n) : ')
if ch in 'Nn':
break
pickle.dump(rec,f)
print('Record Added')
f.close()
def search():
f=open(r'G:\Juliet 2023-2024\students.dat','rb')
r=int(input('Enter Roll no. of student to search : '))
found=0
try:
while True:
d=pickle.load(f)
for i in d:
if i[0]==r:
print('The searched roll no is found : ', i)
found=1
break
except EOFError:
f.close()
if found==0:
print('The searched roll no is not found')
create()
search()