You are on page 1of 6

1 Define the following user-defined functions

a. GetStory(), to create a text file named STORY.TXT, which should contain text lines entered
by the user.
b. Words(), to read and display each word of the text file STORY.TXT in different lines on the
screen.
Also, write menu driven code to call the above functions using a loop.

def GetStory():
with open("STORY.TXT","w") as F:
while True:
T=input("Text:")
F.write(T+"\n")
C=input("More(Y/N)?")
if C=='N':
break
#-----------------------------------------------------#
def Words():
try:
with open("STORY.TXT") as F:
AL=F.read()
for w in AL.split():
print(w)
except:
print("File Not Found!")
#-----------------------------------------------------#
while True:
C=input("G:GetStory W:Words Q:Quit ")
if C=='G':
GetStory()
elif C=='W':
Words()
elif C=='Q':
break
else:
print("Invalid Option")

Page: 1
2 Write a Python code with the following functions performing mentioned operations on a binary
file “CANDIDATE.DAT” containing lists with [<Candidateno of type int>,<Cname of type
string>,<Score of type float>]
a. Enrol(), to add details of new CANDIDATEs from the user and save in “CANDIDATE.DAT”.
b. ShowAll(), to display the entire content of “CANDIDATE.DAT”

Also, write menu driven code to call the above functions using a loop.

import pickle
def Enrol():
with open("CANDIDATE.DAT","wb") as F:
REC=[]
while True:
Cno=int(input("Candidate No:"))
Cname=input("Candidate Name:")
Score=float(input("Score:"))
REC.append([Cno,Cname,Score])
C=input("More(Y/N)?")
if C in 'nN':
Break
pickle.dump(REC,F)
#-----------------------------------------------------#
def ShowAll():
try:
with open("CANDIDATE.DAT","rb") as F:
REC=pickle.load(F)
for R in REC:
print("Candidate No:",R[0])
print("Candidate Name:",R[1])
print("Score:",R[2])
except:
print("File not found!")
#-----------------------------------------------------#
while True:
C=input("E:Enrol S:Show Q:Quit ")
if C=='E':
Enrol()
elif C=='S':
ShowAll()
elif C=='Q':
break
else:
print("Invalid Option")

Page: 2
3 Write a Python code with the following functions performing mentioned operations on a binary
file “ACCOUNTS.DAT” containing lists with [<Ano of type int>,<Name of type string>,<Balance of
type float>]

a. Register(), to add details of new account holders as entered by the user in the binary file
named ACCOUNTS.DAT.
b. DisplayAll( ) to display the details of all Account Holders from ACCOUNTS.DAT

Also, write a menu driven code to call the above functions using a loop

import pickle
def Register():
with open("ACCOUNTS.DAT","wb") as F:
REC=[]
while True:
Ano=int(input("AccountNo:"))
Name=input("Name:")
Bal=float(input("Balance:"))
REC.append([Ano,Name,Bal])
CH=input("More(Y/N)?")
if CH in "nN":
break
pickle.dump(REC,F)

#-----------------------------------------------------#
def DisplayAll():
try:
with open("ACCOUNTS.DAT","rb") as F:
REC=pickle.load(F)
for R in REC:
print(R[0],"|",R[1],"|",R[2])
except:
print("No data in File")
#-----------------------------------------------------#
while True:
ch=input("1. Register 2. Display 3. Exit :")
if ch=="1":
Register()
elif ch=="2":
DisplayAll()
elif ch=="3":
break
else:
print("Invalid Choice")

Page: 3
4 Write a Python code with the following functions performing mentioned operations on a CSV file
“CANDIDATE.CSV” containing lists with [<Candidateno of type int>,<Cname of type string>,<Score
of type float>]

a. Enrol(), to add details of new CANDIDATEs from the user and save in “CANDIDATE.CSV”.
b. ShowAll(), to display the entire content of “CANDIDATE.CSV”

Also, write menu driven code to call the above functions using a loop.

import csv
def Enrol():
with open("CANDIDATE.CSV","a") as F:
W = csv.writer(F)
while True:
Ano=int(input("Candidate No:"))
Name=input("Name:")
Bal=float(input("Score:"))
W.writerow([Ano,Name,Bal])
CH=input("More(Y/N)?")
if CH in "nN":
break
#-----------------------------------------------------#
def ShowAll():
try:
with open("CANDIDATE.CSV","r") as F:
R = csv.reader(F)
for i in R:
print(i[0],"|",i[1],"|",i[2])
except:
print("File Doesn't exist")
#-----------------------------------------------------#
while True:
ch=input("1. Enroll 2. Show 3. Exit >>")
if ch=="1":
Enrol()
elif ch=="2":
ShowAll()
elif ch=="3":
break
else:
print("Invalid Choice")

Page: 4
1] Table: GROWTH
INCHARGE CODE FERTILIZER COST YEARS TYPE
A. Sen 2345 Bimonthly 6000 8 Plantation
L. Goenka 1290 Monthly 3500 2 Agriculture
W. Bhaskar 3345 Yearly 6500 15 Tree
S. Marfatia 6782 Monthly 7900 15 Plantation
R. Naval 2456 Bimonthly 1000 4 Agriculture
Y. Nath 5690 Monthly 2000 19 Plantation
Based on table: GROWTH, write queries:
1. To display the CODE, COST and TYPE in descending order of COST.
SELECT CODE, COST, TYPE FROM GROWTH ORDER BY COST DESC;

2. To display the least number of YEARS amongst the type Plantation.


SELECT MIN(YEARS) FROM GROWTH WHERE TYPE="Plantation";

3. To increase the COST by 100 for all Agriculture TYPE.


UPDATE GROWTH SET COST = COST + 100 WHERE TYPE="Agriculture";

4. To display the names of all INCHARGEs whose code is above 3000.


SELECT INCHARGE FROM GROWTH WHERE CODE>3000;

[2] Table : RYTHM


SINGER CODE ALBUM COST Year TYPE
S. Das 2335 Ruins 26000 1999 Pop
D. Patnaik 1801 Sargam 40000 2000 Classical
N. Rama 1267 Bang 34000 1997 Remix
K. Murthy 6766 Raga 46000 1991 Classical
U. Gopal 2656 Kismet 20000 2000 Pop
I. Beri 1449 Hits 30000 1999 Remix
Based on table RYTHM, write queries:
1. To display ALBUM names of all remixes in ascending order of CODE.
SELECT ALBUM FROM RYTHM WHERE TYPE="Remix" ORDER BY CODE;

2. To display the maximum COST for each TYPE.


SELECT TYPE,MAX(COST) FROM RYTHM GROUP BY TYPE;

3. To change the COST to 30000 wherever the COST is less than 30000.
UPDATE RYTHM SET COST=30000 WHERE COST<30000;

4. To display the names of all SINGERs whose ALBUM was released in the YEAR 2000.
SELECT SINGER FROM RYTHM WHERE YEAR=2000;

[3] Table: CLUB

Page: 5
NAME CODE ACTIVITY CHARGE YEAR TYPE
A. Sengupta 2345 Tennis 6000 1999 Member
L. Patni 1890 Swimming 3500 2000 Resident
N. Patkar 3345 Tennis 6500 1998 Resident
S. Marfatia 6782 Golf 7900 1987 Member
R. Parikh 2456 Golf 9000 2000 Resident
O. Nath 5690 Tennis 2000 1978 Resident
Based on table CLUB, write queries:
1. To display the CODE, ACTIVITY and CHARGE in descending order of CHARGE.
SELECT CODE, ACTIVITY, CHARGE FROM CLUB ORDER BY CHARGE DESC;

2. To display the least CHARGE for every ACTIVITY.


SELECT ACTIVITY, MIN(CHARGE) FROM CLUB GROUP BY ACTIVITY;

3. To count the total number of records for which TYPE is Member.


SELECT COUNT(*) FROM CLUB WHERE TYPE="Member";

4. To display the NAME for all who joined after the YEAR 1980.
SELECT NAME FROM CLUB WHERE YEAR>1980;

[4] Table : ACCESSORIES


DESIGNER CODE CITY CHARGE YEAR DESIGN
A. Sen 2335 CAL 16000 1999 Traditional
L. Pathak 1809 DEL 9900 2000 Modern
N. Krishnan 3395 BNG 16700 2001 Old
K. Lal 6766 DEL 12000 1991 Old
G. Roshan 2656 BOM 13000 2000 Traditional
T. Beri 4490 CAL 30000 1999 Modern
Based on table: ACCESSORIES, write queries:
1. To display the CODE, DESIGN and CHARGE in descending order of DESIGN
SELECT CODE, DESIGN, CHARGE FROM ACCESSORIES ORDER BY DESIGN DESC;

2. To display the maximum CHARGE for each CITY.


SELECT MAX(CHARGE),CITY FROM ACCESSORIES GROUP BY CITY;

3. To display the total of all CHARGE.


SELECT SUM(CHARGE) FROM ACCESSORIES;

4. To display the DESIGNER, whose CITY is either CAL or DEL


SELECT DESIGNER FROM ACCESSORIES WHERE CITY IN ("CAL","DEL");

Page: 6

You might also like