Professional Documents
Culture Documents
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;
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;
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;
4. To display the NAME for all who joined after the YEAR 1980.
SELECT NAME FROM CLUB WHERE YEAR>1980;
Page: 6