You are on page 1of 3

LAB – 8

Q. A text file records.txt contains information of a


student such as roll number, name and marks. Write a
menu driven program which does the following →

● Add the records to the file

● Display the records

● Search for a particular record based on the roll


number

● Quit

fname='records.txt'
def create():
f=open(fname,'a')
s=[]
while True:
rno=input('ROLL NO:')
name=input('NAME:')
m=input('MARKS:')
s.append(rno+','+name+','+m+'\n')
q=input('more records? (Y/N):')
if q.upper()=='N':
break
f.writelines(s)
f.close()
def display():
f=open(fname)
r=f.read()
print(r)
f.close()

def search():
f=open(fname)
no=int(input('enter the number:'))
flag=0
n=f.readline()
for n in f:
i=0
roll_no=''
while True:
if n[i]==',':
break
if n[i]>='0' and n[i]<='9':
roll_no+=n[i]
i+=1
roll_no=int(roll_no)
if roll_no==no:
print(n)
flag=1
break
if flag==0:
print('no such records')
f.close()

while True:
print('1.create records')
print('2.display records')
print('3.search for a record using roll no')
print('4.Quit')
ch=int(input('enter your choice:'))
if ch==1:
create()
elif ch==2:
display()
elif ch==3:
search()
elif ch==4:
break
else:
print('Invalid choice')

You might also like