0% found this document useful (0 votes)
91 views14 pages

Comp Lab Solutions

The document describes a menu driven program to create and manage a binary file containing records of hotel rooms. It includes functions to add records to the file, display all records, and display details of a specific room by room number.

Uploaded by

Izan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views14 pages

Comp Lab Solutions

The document describes a menu driven program to create and manage a binary file containing records of hotel rooms. It includes functions to add records to the file, display all records, and display details of a specific room by room number.

Uploaded by

Izan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SET B

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:"))

rprice=int(input("enter room price:"))

rtype=input("enter room type:")

hotel=[rno,rprice,rtype]

rec.append(hotel)

ch=input("do you want to enter more recs:Y/N")

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')

r=int(input("enter room no:"))

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:

print("sorry.. no record found:")

while True:

print("1: to create a file and to enter records:")

print("2: read or display records:")


print("3: to display specific room details")

print("4:to exit:")

ch=int(input("enter your choice:"))

if ch==1:

addrec()

elif ch==2:

disp()

elif ch==3:

spdisp()

elif ch==4:

break

else:

print("pls enter choice between 1 to 4:")

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:"))

name=input("enter student name:")

grade=input("enter grade:")

percentage=int(input("etner percentage:"))

report=[rno,name,grade,percentage]

rec.append(report)

ch=input("do you want to enter more recs:Y/N")

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:

print("sorry.. no record found:")

while True:

print("1: to create a file and to enter records:")

print("2: read or display records:")

print("3: to display specific student details based on the given percentage")


print("4:to exit:")

ch=int(input("enter your choice:"))

if ch==1:

addrec()

elif ch==2:

disp()

elif ch==3:

spdisp()

elif ch==4:

break

else:

print("pls enter choice between 1 to 4:")

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 = {}

n = int(input("Enter range of dictionary:"))

for i in range(n):

department = input("Enter name of department: ")

pc = int(input("Enter number of computers: "))

company[department] = pc

print("the dictionary is:",company,end="\n")

while True:

print("1. Push:")

print("2: pop and display:")

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:

print(“enter choice between 1 to 3:”)

NOTE:- SET F is similar to SET E

SQL STUB CODES

SET A

#1

import mysqlconnector as sqltor


#2

Mycon=sqltor.connect(host=”localhost”,
user=”root”,password=”123”,database=”hospital”)

#3

Cursor.execute(“select docname,patname from patirnt p, doctor d


where d.docid=p.patid and d.specialist in(“ortho”,”cardio”)”)

#4

Data=cursor.fetchall()

SET B

#1

host=”localhost”,user=”root”,password=”123”,database=”job”

#2

Cursor.execute(“select name,city from dept d, worker w where


d.dcode=w.dcode and w.gender=”female””)

#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

Cursor.execute(“select gamename,name from palyer p ,games g


where g.gcode=p.gcode and g.scheduledate>”2004-03-01” orderby
g.prizemoney”)

#3

Data=fetchall()

#4

Cursor.execute(‘delete from games where gamename=’lawn tennis’ ”)

Mycon.cmmit()
SET D

#1

Mycon=sqltor.connect(host=”localhost”,user=”root”,password=”123”,
database=”company”)

#2

Cursor=mycon.cursor()

#3

Cursor.execute(“select name,hobby from personnel p, job j where


p.empno=j.srno groupby j.dept orderby j. salary desc”)

#4

Data=fetmany(2)

SET E

#1

Import mysqlconnector as sqltor

#2

Cursor=mycon.cursor()

#3

Cursor.execute(“select bookname,author,quantityissued from books


b , issued I where b.bookid=I.bookid groupby b.type”)

#4

data=fetchall()
SET F

#1

Mycon=sqltor.connect(host=”localhost”,user=”root”,password=”123”,
database=”sales”

#2

Cursor=mycon.cursor()

#3

Cursor.execute(“select agentname,phonenumber from agents a,


orders r where a.agentcode=r.agentcode and r.advance_amt>1000”)

#4

Data=fethmany(2)

You might also like