Comp Lab Solutions
Comp Lab Solutions
a) Write a menu driven program to create a binary file “data.dat”, which stores the record of the
“Hotel” in the form list containing room_no, room_price,room_type.
Write functions to achieve the following:
i) Write a function addrec() to add a record in a file
ii) Write a function disp() to display all the records in a file ina tabular form
iii) Write a function to spdisp(room_no) which takes room_no as its argument and displays
its details.
import pickle
def addrec():
f= open('Data.dat','wb')
rec=[]
while True:
rno=int(input("enter roomno:"))
hotel=[rno,rprice,rtype]
rec.append(hotel)
if ch in "Nn":
break
pickle.dump(rec,f)
print("record added:")
f.close()
def disp():
f=open('Data.dat','rb')
try:
while True:
f1=pickle.load(f)
for i in f1:
print(i,end=" ")
print()
except Exception:
f.close()
def spdisp():
f=open('Data.dat','rb')
found=0
try:
while True:
f1=pickle.load(f)
for i in f1:
if i[0]==r:
print(i)
found=1
break
except EOFError:
f.close()
if found==0:
while True:
print("4:to exit:")
if ch==1:
addrec()
elif ch==2:
disp()
elif ch==3:
spdisp()
elif ch==4:
break
else:
SET C
Write a menu driven python program to create a binary file “student.dat” which stores student records
in the form of a list . Write separate functions to a achieve the following:
i) To create and insert records in the list
ii) Display records in tabular form based on the percentage entered by the user list [rno, name, class,
percentage]
import pickle
def addrec():
f= open('stud.dat','wb')
rec=[]
while True:
rno=int(input("enter rollo:"))
grade=input("enter grade:")
percentage=int(input("etner percentage:"))
report=[rno,name,grade,percentage]
rec.append(report)
if ch in "Nn":
break
pickle.dump(rec,f)
print("record added:")
f.close()
def disp():
f=open('stud.dat','rb')
try:
while True:
f1=pickle.load(f)
for i in f1:
print(i,end=" ")
print()
except Exception:
f.close()
def spdisp():
f=open('stud.dat','rb')
percentage=int(input("enter percentage:"))
found=0
try:
while True:
f1=pickle.load(f)
for i in f1:
if i[3]==percentage:
print(i)
found=1
break
except EOFError:
f.close()
if found==0:
while True:
if ch==1:
addrec()
elif ch==2:
disp()
elif ch==3:
spdisp()
elif ch==4:
break
else:
SET E
A company having dictionary of various Departments and Number of computers (PC) available as key
value pairs. Write a menu driven program, with separate user defined functions to perform the
following operations:
● Push the keys (name of the Department) of the dictionary into a stack, where the corresponding value
(Number of PC) is 25 or more.
● Pop and display the content of the stack
Company={“HR”:10,”Quality”:75,”Support”:50,”Production”:20,”Supply”:85}
def push(st,c):
st.append(c)
def pop(st):
while True:
if st!= []:
return st.pop()
else:
return None
def display(st):
while True:
if st!=[]:
print(pop(st),end=" ")
st=[]
company = {}
for i in range(n):
company[department] = pc
while True:
print("1. Push:")
print("3: quit")
ch = int(input("Enter your choice:"))
if ch == 1:
st=[]
for i in company:
if company[i]>=25:
push(st,i)
elif ch==2:
while True:
if st!=[]:
print(pop(st),end=” “)
elif ch==3:
break
else:
SET A
#1
Mycon=sqltor.connect(host=”localhost”,
user=”root”,password=”123”,database=”hospital”)
#3
#4
Data=cursor.fetchall()
SET B
#1
host=”localhost”,user=”root”,password=”123”,database=”job”
#2
#3
Data=fetchall()
#4
Cursor.execute(“insert into
dept(dcode,department,city)values(‘{}’,’{}’,’{}’.format(“D06”,”sales”,”
Nagpur”)
Mycon.commit()
SET C
#1
Sql.connect(host=”localhost”,user=”root”,password=”123”,database=
”games”)
#2
#3
Data=fetchall()
#4
Mycon.cmmit()
SET D
#1
Mycon=sqltor.connect(host=”localhost”,user=”root”,password=”123”,
database=”company”)
#2
Cursor=mycon.cursor()
#3
#4
Data=fetmany(2)
SET E
#1
#2
Cursor=mycon.cursor()
#3
#4
data=fetchall()
SET F
#1
Mycon=sqltor.connect(host=”localhost”,user=”root”,password=”123”,
database=”sales”
#2
Cursor=mycon.cursor()
#3
#4
Data=fethmany(2)