You are on page 1of 27

And do the following operations using menu driven program.

The menu is as
follows:

print("1 start_i( )— -how many words start with ‘I”


print("2 count_is")-count how many “is” are there
print("3 four letterword")—how many four letter words are there. print("4.move
pointert ")-for moving the pointer
print("5 EXIT")—for coming out the menu

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

def four_letterword(): def move_pointer():


f=open("series1.txt","r") print("showing the working of
st=f.readlines() seek() functions ")
print(st) f=open("series1.txt","rb")
count=0 f.seek(0,0)
for data in st: s=f.read(1) print(s)
data=data.strip() print("reading entire file and printing the
data=data.split() length")
for i in data: st=f.readline()
if len(i)==4: st=f.readline()
print(i) st=f.readlines()
count=count+1 print(st)
print("number of 4 letter
words",count)
print(st)
c=len(st)
print("lenght of the string",c)
f.seek(2)
c=f.read(3)
print(c)
tn=f.tell() print("5 EXIT")
print("The position of the choice=int(input("enter the
9. Write
pointer",tn) a menu program to create the binary file item.dat with item id, item
choice"))
name, itemthe
print("moving price, item quantity, and write the list object into to the file and do
pointer
from thethecurrent
following operations.
position")
1. Writefile()
print(f.seek(5,1)) if choice==1:
2. Readfile()
print("Trapping seek") start_i()
3. Searchrecord()
print(c) if choice==2: four_letterword()
4. Countrecords() if choice==3:
5. Exit
c=f.read(2) count_is()
print(c) if choice==4:
import pickle
tn=f.tell() move_pointer()
import sys
print("The position of the
import os
pointer",tn) if choice==5:
print("no more operations")
defprint("moving
writefile_list():
the pointer print("thankyou")
fw=open("cloth.dat","wb")
from the end") break
n=int(input("how
f.seek(-6,2) many
records"))
c=f.read(4)
print(c)
l=[0 for i in range(n)]
tn=f.tell()
for i in range(n):
print("The position of the
pointer",tn)
f.seek(0,2)

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

print("3 search record")


print("4 count record")
ch=int(input("enter the choice"))

if ch==1:
writefile_list()
elif ch==2:
readfile_list()
elif ch==3:
searchrecord()
elif ch==4:
countrecord()

reply=input("wish to continue y-for


yes n for no")

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

import pickle OUTPUT


import os,sys
bd={} Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30
def writefile_dict(): 2021, 20:19:38) [MSC v.1929 64 bit
ans='y' (AMD64)] on win32
fw=open("book.dat","wb") Type "help", "copyright", "credits" or
while ans=='Y' or ans=='y': "license()" for more information.
bd={} >>>
bno=int(input("enter Bid no:")) === RESTART: F:\PYTHON\FILE\
bnm=input("accept book MANIPULATE_BINARY
name") FILE_DICTIONARY.py ===
bquan=int(input("accept menu
quantity")) 1.write binaryfile
bprice=float(input("accept book 2 read binaryfile
price")) 3 searchrecord
bd['no']=bno 4 count records
bd['name']=bnm 5 update record
bd["quantity"]=bquan 6 delete record
bd['price']=bprice enter the choice2
pickle.dump(bd,fw) {'no': 1005, 'name': 'wings of fire', 'quantity':
ans=input("do you wish to 20, 'price': 200.95}
continue if you wish accept small or <class 'dict'>
capital y else accept anykey") {'no': 1001, 'name': 'school', 'quantity': 39,
fw.close() 'price': 135.98}
<class 'dict'>
Ran out of input
def readfile_dict(): menu
with open("book.dat","rb")as f: 1.write binaryfile
while True: 2 read binaryfile
try: 3 searchrecord
r=pickle.load(f) 4 count records
print(r) 5 update record
print(type(r)) 6 delete record
except Exception as e: enter the choice3
print(e) whose bid to be searched and printed1001
break <class 'int'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'int'>
def searchrecord(): 1001
id=int(input("whose bid to be {'no': 1001, 'name': 'school', 'quantity': 39,
searched and printed")) 'price': 135.98}
<class 'str'>
count=0 <class 'int'>
with open("book.dat","rb")as f: <class 'float'>
while True: menu
try: 1.write binaryfile
r=pickle.load(f) 2 read binaryfile
#for a in r.values(): 3 searchrecord
for a in r.values(): 4 count records
print(type(a)) 5 update record
6 delete record
if a==id: enter the choice4
print(a) {'no': 1005, 'name': 'wings of fire', 'quantity':
print(r) 20, 'price': 200.95}
count=count+1 <class 'dict'>
except EOFError: {'no': 1001, 'name': 'school', 'quantity': 39,
break 'price': 135.98}
if count==0: <class 'dict'>
print("not found") no of records are 2
menu
1.write binaryfile
def deleterecord(): 2 read binaryfile
id=int(input("whose bid to be 3 searchrecord
searched and deleted")) 4 count records
# bnm=input("enter the bookname 5 update record
to be searched") 6 delete record
f1=open("temp.dat","wb") enter the choice
count=0
with open("book.dat","rb")as f:
while True:
count=0
try:
r=pickle.load(f)
for a in r.values():
if a==id:
print(a)
print("record
deleted")
count=count+1
if count==0:
pickle.dump(r,f1)
except EOFError:
break
f1.close()

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'

while reply=='Y' or reply=='y':


print("menu")
print("1.write binaryfile")
print("2 read binaryfile")
print("3 searchrecord ")
print("4 count records")
print("5 update record")
print("6 delete record")
ch=int(input("enter the choice"))

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)

for row in csvr:


if id!=row[0]:
csvw.writerow(row)
if id==row[0]:
k=1

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;

reply=input("do you wish to


continue type yes for continue")

12. Write a menu driven program to show how text file can be opened in different
modes

import MENU.m Output


def writemode(): Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021,
20:19:38) [MSC v.1929 64 bit (AMD64)] on
win32
f=open(r"f:\..\..\modes1.txt","w") Type "help", "copyright", "credits" or "license()"
n=int(input("how many for more information.
strings")) >>>
for i in range(n): ====== RESTART: F:\PYTHON\FILE\
st=input("accept") MENU_DRIVEN_TEXTFILE
f.write(st+'\n') _MODES.py ======
f.close() 1 write mode-w
2 read mode()
def readmode(): 3 append mode-a
f=open("modes.txt","r") 4 write mode-w+)
st=f.readlines() 5 read mode-r+
print(st) 6 append mode a+
for i in st: 7 exit mode
i=i.strip() Accept choice2
print(i) ['slow and steady wins the race\n', 'better late than
f.close() never\n']
slow and steady wins the race
def appendmode(): better late than never
f=open("modes.txt","a") 1 write mode-w
st="all modes need to 2 read mode()
understood thoroughly" 3 append mode-a
f.write(st) 4 write mode-w+)
print("appended successfully") 5 read mode-r+
f.close() 6 append mode a+
7 exit mode
def writeplusmode(): Accept choice6
slow and steady wins the race
f=open("modes.txt","w+") better late than never
n=int(input("how many this is about all 6 modes of text file
strings"))
for i in range(n): 1 write mode-w
st=input("accept") 2 read mode()
f.write(st+'\n') 3 append mode-a
4 write mode-w+)
f.seek(0) 5 read mode-r+
st=f.read() 6 append mode a+
print(st) 7 exit mode
f.close() Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
def readplusmode(): this is about all 6 modes of text file
f=open("modes.txt","r+")
st=f.read() 1 write mode-w
print(st) 2 read mode()
f.write("is it possible") 3 append mode-a
f.seek(0) 4 write mode-w+)
st=f.read() 5 read mode-r+
print(st) 6 append mode a+
7 exit mode
f.close() Accept choice6
slow and steady wins the race
def appendplusmode(): better late than never
f=open("modes.txt","a+") this is about all 6 modes of text file
st="this is about all 6 modes this is about all 6 modes of text file
of text file" this is about all 6 modes of text file
f.write(st+'\n')
f.seek(0) 1 write mode-w
st=f.read() 2 read mode()
print(st) 3 append mode-a
f.close() 4 write mode-w+)
5 read mode-r+
def thankyou(): 6 append mode a+
print("thank you") 7 exit mode
Accept choice6
slow and steady wins the race
better late than never
this is about all 6 modes of text file
ans=True this is about all 6 modes of text file
while (ans): this is about all 6 modes of text file
MENU.m.text_modes() this is about all 6 modes of text file
choice=int(input("Accept
choice")) 1 write mode-w
if choice==1: 2 read mode()
writemode() 3 append mode-a
if choice==2: 4 write mode-w+)
readmode() 5 read mode-r+
if choice==3: 6 append mode a+
appendmode() 7 exit mode
if choice==4: Accept choice
writeplusmode()
if choice==5:
readplusmode()
if choice==6:
appendplusmode()
if choice==7:
thankyou()
ans=False

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

Pop() should pop out the peek element i.e 1006


OUTPUT

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:

BOOK={“Wings of fire“: 400, ”The leader”:135, “Broken heart”:125,”name


sake”:135,”Beyond blue mountains”:165,”chicken soup”:75}

The output from the program after 1st pop should be

Beyond blue mountains

and the status of the stack should be as follows

Wings of fire, the leader, name sake,

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

You might also like