Professional Documents
Culture Documents
The menu is as
follows:
import os f.close()
def start_i():
c=0 def count_is():
f=f=open("series1.txt","r") f=open("series1.txt","r")
records=f.readlines() st=f.readlines()
print(records) print(st)
for record in records: count=0
record=record.strip() for data in st:
record=record.split() data=data.strip()
for data in record: print(data)
if data[0]=='i' or data=data.split() print(data)
data[0]=='I': for i in data:
print(data) if i=="is":
c=c+1 print(i)
print("number of words which count=count+1
start with" ,"i","are",c) print("number of is words are ",count)
f.close() f.close()
choice=0
print("menu for creation of file
and doing operations")
while choice<=5:
print("do you wish to carry on
with file manipulation ")
print("menu")
print("1 words_i ")
print("2 count_is")
print("3 four letterword")
print("4 move_pointer")
l=[]
cid=int(input("enter the clot cnm=(input("enter fabric"))
id")) cprice=float(input("accept
price"))
l.extend([cid,cnm,cprice])
pickle.dump(l,fw)
fw.close()
def readfile_list():
f=open("cloth.dat","rb")
while True:
try:
r=pickle.load(f)
print(r)
except EOFError:
sys.stderr.write("end of the
file reached")
break
f.close()
def countrecord():
count=0
f=open("cloth.dat","rb")
while True:
try:
r=pickle.load(f)
count=count+1
except EOFError:
break
f.close()
print("no of records",count)
def searchrecord():
id=int(input("enter the cloth id to
be searched"))
count=0
with open("cloth.dat","rb")as f:
while True:
try:
r=pickle.load(f)
for a in r:
if a==id:
print(a)
count=count+1
print(r)
except EOFError:
break
if count==0:
print("not found")
reply='y'
while reply=='Y' or reply=='y':
print("menu")
print("1.write binaryfile")
print("2 read binaryfile")
if ch==1:
writefile_list()
elif ch==2:
readfile_list()
elif ch==3:
searchrecord()
elif ch==4:
countrecord()
10. Write a menu program to create the binary file book.dat with dictionary object
which contains bid,bnm,bprice,bquantity and do the following operations.
1. Writefile
2. Readfile
3. Searchrecord()
4. Delete record()
5. updaterecord()
6. Countrecord()
7. Exit
os.remove("book.dat")
os.rename ("temp.dat","book.dat")
def countrecords():
nor=0
with open("book.dat","rb")as f:
while True:
try:
r=pickle.load(f)
print(r)
nor=nor+1
print(type(r))
except EOFError:
break
print("no of records are",nor)
f.close()
def updaterecord():
no=int(input("enter which book
id's details to be updated"))
count=0
with open("book.dat",'rb+') as f:
while True:
try:
p=f.tell()
print(p)
r=pickle.load(f)
for a in r.values():
if a==no:
count=count+1
nm=input("enter book
name")
price=float(input("enter price"))
r["name"]=nm
r["price"]=price
f.seek(p)
pickle.dump(r,f)
except EOFError:
break
if count==0:
print("no record")
else:
print("record updated")
f.close()
reply='y'
if ch==1:
writefile_dict()
elif ch==2:
readfile_dict()
elif ch==3:
searchrecord()
elif ch==4:
countrecords()
elif ch==5:
updaterecord()
elif ch==6:
deleterecord()
reply=input("wish to continue y-for
yes n for no")
11. Write a menu driven to print the following the menu and do the following
operationsm on csv file
1 write csvfile
2 read csvfile
3 count csvfile
4 search record
5 delete record
6 update record
7 sort record
8 exit the screen
OUTPUT
import csv
import os Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021,
fields = ['id','item name','item 20:19:38) [MSC v.1929 64 bit (AMD64)] on
price','item quantity'] win32
print("this is to create csv file")
Type "help", "copyright", "credits" or "license()"
fn="ITEM.csv" for more information.
def writefile_csv(): >>>
item=[] ========== RESTART:
csvfilew = open(fn,'w') F:\PYTHON\FILE\MANIPULATE_LIST_CSV.p
y =========
csvw=csv.writer(csvfilew,lineter this is to create csv file
minator='\n') menu
#csvw=csv.writer(csvfilew) 1 write csvfile
csvw.writerow(fields) 2 read csvfile
ans='yes' 3 count csvfile
while ans=='yes' or 4 search record
ans=='YES': 5 delete record
id=input("enter item id") 6 update record
iname=input("enter item 7 sort record
name") 8 exit the screen
iprice=float(input("enter accept the choice2
price")) fetch the data and print
iquantity=int(input("enter ['1001', 'nuts', '20.0', '20']
quantity")) ['1000', 'bolts', '30.0', '30']
do you wish to continue type yes for continueyes
menu
item.extend([id,iname,iprice,iqua 1 write csvfile
ntity]) 2 read csvfile
#print(item) 3 count csvfile
csvw.writerow(item) 4 search record
item=[] 5 delete record
ans=input("do you wish to 6 update record
continue type yes-for yes n - for 7 sort record
no") 8 exit the screen
csvfilew.close() accept the choice3
['1001', 'nuts', '20.0', '20']
def readfile_csv(): ['1000', 'bolts', '30.0', '30']
print("fetch the data and number of records 2
print") do you wish to continue type yes for continueyes
with open(fn,'r')as csvfiler: menu
csvr=csv.reader(csvfiler) 1 write csvfile
for r in csvr: 2 read csvfile
print(r) 3 count csvfile
#csvfiler.close() 4 search record
5 delete record
def count_csv(): 6 update record
c=-1 7 sort record
with open(fn,'r')as csvfiler: 8 exit the screen
csvr=csv.reader(csvfiler) accept the choice4
for r in csvr: enter the id which you want search1000
print(r) <_csv.reader object at 0x00000199E1005520>
c=c+1 yes found
print("number of records",c) ['1000', 'bolts', '30.0', '30']
do you wish to continue type yes for continue
def delete_record():
csvfilew =
open("newitem.csv",'w',newline
="")
csvw=csv.writer(csvfilew)
#csvw.writerow(fields)
id=input("enter the id")
k=0
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
if k==0:
print("no matching record
found could not delete
anything")
csvfilew.close()
csvfiler.close()
os.remove("ITEM.csv")
os.rename("newitem.csv","ITEM
.csv")
def sort():
item=[]
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
i=0
for row in csvr:
if i>0:
item.append(row)
i=i+1
print("fetch and print")
print(item)
for i in range(0,len(item)):
for j in range(0,len(item)-
1-i):
if (int)(item [j]
[0])>(int)(item[j+1][0]):
item[j],item[j+1]=item[j+1],item
[j]
print("after sorting")
print(item)
#fields = ['id','item
name','item price','item quantity']
csvfilew1 =
open("newit.csv",'w')
csvw=csv.writer(csvfilew1,linete
rminator='\n')
csvw.writerow(fields)
csvw.writerows(item)
csvfilew1.close()
csvfiler.close()
os.remove("ITEM.csv")
os.rename("newit.csv","ITEM.cs
v")
def update_record():
#fields = ['id','item name','item
price','item quantity']
item=[]
id=input("accept id to be
updated")
csvfilew =
open("newu.csv",'w',newline="")
csvw=csv.writer(csvfilew)
with open("ITEM.csv",'r')as
csvfiler:
csvr=csv.reader(csvfiler)
for row in csvr:
if id!=row[0]:
csvw.writerow(row)
elif id==row[0]:
id=int(input("enter
item id"))
iname=input("enter
item name")
iprice=int(input("enter
price"))
iquantity=int(input("enter
quantity"))
item.extend([id,iname,iprice,iqua
ntity])
csvw.writerow(item)
#csvfiler.close()
csvfilew.close()
os.remove("ITEM.csv")
os.rename("newu.csv","ITEM.cs
v")
def search_record():
id=input("enter the id which
you want search")
k=0
with open(fn,'r',newline='\
r\n')as csvfiler:
csvr=csv.reader(csvfiler)
print(csvr)
for row in csvr:
if id==row[0]:
k=k+1
print("yes found")
print(row)
break
if k==0:
print("no matching
record")
#csvfiler.close()
reply='yes'
while reply=="YES" or
reply=="yes":
print("menu")
print("1 write csvfile")
print("2 read csvfile")
print("3 count csvfile")
print("4 search record")
print("5 delete record")
print("6 update record")
print("7 sort record")
print("8 exit the screen")
ch=int(input("accept the
choice"))
if ch==1:
writefile_csv()
elif ch==2:
readfile_csv()
elif ch==3:
count_csv()
elif ch==4:
search_record()
elif ch==5:
delete_record()
elif ch==6:
update_record()
elif ch==7:
sort()
elif ch==8:
print("thank you")
break
else:
continue;
12. Write a menu driven program to show how text file can be opened in different
modes
13. lpha Company has created a dictionary Emp which stores eid and his salary
as key value pairs.The dictionary Emp contains the following key value
pairs
Emp={1001:45000,1002:56000,1003:65000,1004:90000,1005:46000,1006:
55000}
Define the following user defined functions with the help of menu driven program tto
push and pop values.
Push()-To push the keys(eid) of the dictionary into stack, where the corresponding
values(salary) is greaterthan 50000. After pushing, the keys in the stack area as
follow.
1002,1003,1004,1006
emp={1001:45000,1002:56000,1 MENU
003:65000,1004:90000,1005:460 1. PUSH
00,1006:55000} 2. POP
3. DISPLAY
4 peek
c=len(emp) 5. EXIT
print(c) accept which operation?1
sorry could not push as not matching criteria
ch=True sorry could not push as not matching criteria
p=0 [1002, 1003, 1004, 1006]
st=[] MENU
1. PUSH
def push(): 2. POP
for k in emp: 3. DISPLAY
if emp[k]>50000: 4 peek
st.append(k) 5. EXIT
else: accept which operation?4
print("sorry could not the peek element is 1006
push as not matching criteria") MENU
print(st) 1. PUSH
2. POP
def poping(): 3. DISPLAY
if (len(st)==0): 4 peek
print("empty stack so 5. EXIT
nothing can be poped out") accept which operation?5
return False
else:
v=st.pop()
return v
def display():
if len(st)==0:
print("nothing is for display
because the stack is empty")
else:
for i in range((len(st)-1),-1,-
1):
print(st[i])
def peek():
if len(st)==0:
print("no peek element as
nothing is in stack")
else:
print("the peek element
is",st[len(st)-1])
while(ch):
print("MENU")
print("1. PUSH")
print("2. POP")
print("3. DISPLAY")
print("4 peek")
print("5. EXIT")
op=int(input("accept which
operation?"))
if op==1:
push()
elif op==2:
val=poping()
if val==False:
print("nothing in stack
for poping")
else:
print("the value
whichever is poped",val)
elif op==3:
display()
elif op==4:
peek()
elif op==5:
break;
else:
print("wrong choice")
14. The owner of the cinema hall ordered the gate man to collect all the tickets of the audience and
then asked him to provide all tickets which are divisible by 2 for the purpose of lucky draw. He
entered all the ticket numbers in the form of a list. Now You need to help him create a menu
program with separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push all those numbers which are divisible by 2 into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
N=[1211, 1300, 3224, 5660, 2111, 7987, 9800, 2222, 3500, 3800]
Sample Output of the code should displaly 3800 after 1st pop
and after that the status of the stack should be as follows:
[1300,3224,5660,9800,2222,3500]
OUTPUT:-
n=int(input("how many"))
sl=[0 for i in range(n)] how many10
for i in range(n): accept ticket no1211
tno=int(input("accept ticket no")) accept ticket no1300
sl[i]=tno accept ticket no3224
print(sl) accept ticket no5660
ch=True accept ticket no2111
p=0 accept ticket no7987
st=[] accept ticket no9800
accept ticket no2222
def push(): accept ticket no3500
global p accept ticket no3800
if sl[p]%2==0: [1211, 1300, 3224, 5660, 2111, 7987, 9800,
st.append(sl[p]) 2222, 3500, 3800]
print("the pushed element MENU
is",st[::-1]) 1. PUSH
print(st) 2. POP
else: 3. DISPLAY
print("sorry could not push as 4 peek
not matching criteria") 5. EXIT
p=p+1 accept which operation?1
sorry could not push as not matching criteria
def poping(): MENU
if (len(st)==0): 1. PUSH
print("empty stack so nothing 2. POP
can be poped out") 3. DISPLAY
return False 4 peek
else: 5. EXIT
v=st.pop() accept which operation?1
return v the pushed element is 1300
[1300]
def display(): MENU
if len(st)==0: 1. PUSH
print("nothing is for display 2. POP
because the stack is empty") 3. DISPLAY
else: 4 peek
for i in range((len(st)-1),-1,-1): 5. EXIT
print(st[i]) accept which operation?1
the pushed element is 3224
def peek(): [1300, 3224]
if len(st)==0: MENU
print("no peek element as 1. PUSH
nothing is in stack") 2. POP
else: 3. DISPLAY
print("the peek element 4 peek
is",st[len(st)-1]) 5. EXIT
accept which operation?1
the pushed element is 5660
[1300, 3224, 5660]
while(ch): MENU
print("MENU") 1. PUSH
print("1. PUSH") 2. POP
print("2. POP") 3. DISPLAY
print("3. DISPLAY") 4 peek
print("4 peek") 5. EXIT
print("5. EXIT") accept which operation?1
sorry could not push as not matching criteria
op=int(input("accept which MENU
operation?")) 1. PUSH
if op==1: 2. POP
if p==n: 3. DISPLAY
print("no more elements") 4 peek
5. EXIT
else: accept which operation?1
push() sorry could not push as not matching criteria
elif op==2: MENU
val=poping() 1. PUSH
if val==False: 2. POP
print("nothing in stack for 3. DISPLAY
poping") 4 peek
else: 5. EXIT
print("the value whichever is accept which operation?1
poped",val) the pushed element is 9800
elif op==3: [1300, 3224, 5660, 9800]
display() MENU
elif op==4: 1. PUSH
peek() 2. POP
elif op==5: 3. DISPLAY
break; 4 peek
else: 5. EXIT
print("wrong choice") accept which operation?1
the pushed element is 2222
[1300, 3224, 5660, 9800, 2222]
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?1
the pushed element is 3500
[1300, 3224, 5660, 9800, 2222, 3500]
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?1
no more elements
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?3
3500
2222
9800
5660
3224
1300
MENU
1. PUSH
2. POP
3. DISPLAY
4 peek
5. EXIT
accept which operation?
15. Vivek has created a dictionary Book which stores Book name and its
price as key value pairs for 5 products. Write a menu driven program
with user defined functions to perform the following operations:
● Push the keys (name of the Book ) of the dictionary into a stack,
where the corresponding value (price) is greater than Rs.125/-
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
OUTPUT
bd={"wings of fire":150, "name STRUCTURES\stack_books.PY
sake":100, "beyond the blue ========
moutains":200,"the leader":145} 4
c=len(bd) MENU
print(c) 1. PUSH
2. POP
ch=True 3. DISPLAY
p=0 4 peek
st=[] 5. EXIT
accept which operation?1
def push(): sorry could not push as not matching
for k in bd: criteria
if bd[k]>125: ['wings of fire', 'beyond the blue
st.append(k) moutains', 'the leader']
else: MENU
print("sorry could not push as not 1. PUSH
matching criteria") 2. POP
print(st) 3. DISPLAY
4 peek
def poping(): 5. EXIT
if (len(st)==0): accept which operation?3
print("empty stack so nothing can be the leader
poped out") beyond the blue moutains
return False wings of fire
else: MENU
v=st.pop() 1. PUSH
return v 2. POP
3. DISPLAY
def display(): 4 peek
if len(st)==0: 5. EXIT
print("nothing is for display because accept which operation?4
the stack is empty") the peek element is the leader
else: MENU
for i in range((len(st)-1),-1,-1): 1. PUSH
print(st[i]) 2. POP
3. DISPLAY
def peek(): 4 peek
if len(st)==0: 5. EXIT
print("no peek element as nothing is accept which operation?2
in stack") the value whichever is poped the leader
else: MENU
print("the peek element is",st[len(st)- 1. PUSH
1]) 2. POP
3. DISPLAY
4 peek
5. EXIT
while(ch): accept which operation?3
print("MENU") beyond the blue moutains
print("1. PUSH") wings of fire
print("2. POP") MENU
print("3. DISPLAY") 1. PUSH
print("4 peek") 2. POP
print("5. EXIT") 3. DISPLAY
4 peek
op=int(input("accept which 5. EXIT
operation?")) accept which operation?
if op==1:
push()
elif op==2:
val=poping()
if val==False:
print("nothing in stack for poping")
else:
print("the value whichever is
poped",val)
elif op==3:
display()
elif op==4:
peek()
elif op==5:
break;
else:
print("wrong choice")