Programs 11-17
Programs 11-17
import pickle
def create():
f=open("traveldetails.dat","wb")
n=int(input("Enter how many travel tickets you want:"))
for i in range(n):
travel_id=int(input("Enter Travel ID:"))
start=input("Enter Start Journey:")
end=input("Enter End Journey:")
price=int(input("Enter Ticket Price:"))
L=[travel_id,start,end,price]
pickle.dump(L,f)
if (f):
print("File Created Successfully")
else:
print("Error Occured")
f.close()
def display():
f=open("traveldetails.dat","rb")
try:
print(" ID " , "Start " , "End " , "Price " )
while True:
det=pickle.load(f)
print(" ",det[0]," ",det[1]," ",det[2]," ",det[3])
except:
f.close()
def update(travel_id,price):
f=open("traveldetails.dat","rb+")
flag=False
try:
while True:
recpos=f.tell()
det=pickle.load(f)
if det[0]==travel_id:
det[-1]=price
f.seek(recpos)
pickle.dump(det,f)
flag=True
except:
if flag==False:
print("Error Occured")
else:
print("File Updated Successfully")
f.close()
ans="y"
while ans.lower()=="y":
print("1.Perform To Create A Binary File")
print("2.Perform To Display The File")
print("3.Perform Updating The File")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
travel_id=int(input("Enter Travel ID To Be Updated:"))
price=int(input("Enter Ticket Price:"))
update(travel_id,price)
ans=input("Would you like to continue:(y/n)")
Output
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:1
Enter how many travel tickets you want:3
Enter Travel ID:0001
Enter Start Journey:mas
Enter End Journey:del
Enter Ticket Price:6000
Enter Travel ID:0002
Enter Start Journey:del
Enter End Journey:mas
Enter Ticket Price:8000
Enter Travel ID:0003
Enter Start Journey:nyc
Enter End Journey:mas
Enter Ticket Price:12000
File Created Successfully
Would you like to continue:(y/n)y
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:2
ID Start End Price
1 mas del 6000
2 del mas 8000
3 nyc mas 12000
Would you like to continue:(y/n)y
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:3
Enter Travel ID To Be Updated:0001
Enter Ticket Price:9000
File Updated Successfully
Would you like to continue:(y/n)y
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:2
ID Start End Price
1 mas del 9000
2 del mas 8000
3 nyc mas 12000
Would you like to continue:(y/n)n
BINARY FILE - 2
import pickle
def create():
try:
f=open("binary2.dat",'w')
n=int(input("enter no of rows : "))
while n :
d={}
w=input("enter word : ")
s=input("enter meaning: ")
d[w]=s
pickle.dump(d,f)
except:
f.close()
print("File is created")
def display():
try:
f=open("binary2.dat",'r')
while f:
l=pickle.load(f)
for i in l :
print(i,sep=' : ')
print()
except:
f.close()
print("END OF FILE")
def search():
try:
f1=open("binary2",'r')
n=input("enter word : ")
k=None
while f1:
d=pickle.load(f1)
if n in d:
k=d[n]
print('meaninng of the word is : ')
print(k)
except:
f1.close()
ans='y'
while ans=='y':
create()
a=int(input("""1. display
2.search
your choise :"""))
if a==1:
display()
if a==2:
search()
ans=input("do you want to countinue (y/n) : ")
Output
import csv
def create():
with open('emp1.csv','w',newline='')as f:
w=csv.writer(f,delimiter=':')
ans='y'
l=["employee id","employee name","salary","designation"]
w.writerow(l)
while ans=='y':
eid=int(input("enter employee id : "))
name=input("enter name : ")
sal=int(input("enter salary : "))
des=input("enter designation : ")
l=[eid,name,sal,des]
w.writerow(l)
print("data stored")
ans=input("add more?(y/n) : ")
else:
print("file created")
def display():
with open('emp1.csv','r')as f:
r=csv.reader(f)
for l in r:
for i in l:
print(i,end=' ')
print()
def search():
givid=int(input("Enter required id :"))
with open('emp1.csv','r+')as f:
r=csv.reader(f)
for l in r:
if l[0]==givid:
print(l)
else:
print("record not found")
ans='y'
while ans=='y':
print("1.Create a CSV file, 2.Display the content file, 3.Search on basis of employee id")
a=int(input("your choice: "))
if a==1:
create()
if a==2:
display()
if a==3:
search()
ans=input("do you want to countinue (y/n) : ")
Output
def display():
with open('student.csv','r') as f:
r = csv.reader(f)
print('id' ,'name', 'Sum of all the marks','avg. marks', sep='\t')
for i in r:
print(i[0], i[1], i[2], i[3], sep='\t')
def search(sd):
with open('student.csv','r') as f:
r = csv.reader(f)
for i in r:
if int(i[0])==sd:
print('Total marks of',i[1],'is',i[-2])
print('avrage marks of',i[1],'is',i[-1])
ans="y"
while ans.lower()=="y":
print("1.Create A CSV File")
print("2.Display The Content File")
print("3.Search Based On StudentID")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
sd=int(input('enter student Id: '))
search(sd)
ans=input("Would you like to continue:(y/n)")
RANDOM – 1
import random
def ggame():
print("Welcome to the Random Number Guessing Game!")
print("Think of a number between 1 and 100.")
while True:
try:
ggame()
OUTPUT:
def isEmpty(stk):
if len(stk)==0:
return True
else:
return False
def PUSH(stk,ele):
stk.append(ele)
def POP(stk):
if isEmpty(stk):
return None
else:
item=stk.pop()
return item
def PEEK(stk):
if isEmpty(stk):
print("Stack is empty! Underflow!")
else:
top=len(stk)-1
print("The topmost element is:",stk[top])
def DISPLAY(stk):
if isEmpty(stk):
print("Stack is empty!")
else:
top=len(stk)-1
print(stk[top],"<----TOP")
for i in range(top-1,-1,-1):
print(stk[i])
print("End of Stack")
stack=[]
while True:
print("1.PUSH-Enter elements into a stack")
print("2.POP-Delete element of the stack")
print("3.PEEK-See the topmost element")
print("4.DISPLAY-Display the stack")
print("5.EXIT")
ch=int(input("Enter your choice:"))
if ch==1:
n=int(input("Enter the number of elements you want to enter:"))
for i in range(n):
element=eval(input("Enter the element:"))
PUSH(stack,element)
elif ch==2:
delele=POP(stack)
if delele==None:
print("Stack is empty!! Underflow!")
else:
print("Deleted Item is:",delele)
elif ch==3:
PEEK(stack)
elif ch==4:
DISPLAY(stack)
elif ch==5:
print("Exited!")
break
else:
print("Invalid choice:")
OUTPUT:
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:1
Enter the number of elements you want to enter:5
Enter the element:100
Enter the element:200
Enter the element:300
Enter the element:400
Enter the element:500
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:4
500 <----TOP
400
300
200
100
End of Stack
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:2
Deleted Item is: 500
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:4
400 <----TOP
300
200
100
End of Stack
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:3
The topmost element is: 400
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:4
400 <----TOP
300
200
100
End of Stack
1.PUSH-Enter elements into a stack
2.POP-Delete element of the stack
3.PEEK-See the topmost element
4.DISPLAY-Display the stack
5.EXIT
Enter your choice:5
Exited!
SEVENTEENTH PROGRAM
Book=[]
Message=["Stack is Empty!","Book Added","Book Deleted!","Underflow!"]
def Pushbook():
bkname=input("Enter the book title:")
price=int(input("Enter the price of the book:"))
Book.append([bkname,price])
print(Message[1])
def Popbook():
if Book==[]:
print(Message[3])
else:
Book.pop()
print(Message[2])
def dispbook():
if Book==[]:
print(Message[0])
else:
top=len(Book)-1
print(Book[top],"<----TOP")
for i in range(top-1,-1,-1):
print(Book[i])
while True:
print("1.Add new book(s)")
print("2.Delete a book")
print("3.Display the books")
print("4.EXIT")
ch=int(input("Enter your choice:"))
if ch==1:
n=int(input("Enter the number of books you want to enter:"))
for i in range(n):
Pushbook()
elif ch==2:
Popbook()
elif ch==3:
dispbook()
elif ch==4:
print("Exited!")
break
else:
print("Invalid choice:")
OUTPUT:
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:1
Enter the number of books you want to enter:3
Enter the book title:Famous Five
Enter the price of the book:500
Book Added
Enter the book title:Secret Seven
Enter the price of the book:700
Book Added
Enter the book title:Gernimo Stilton
Enter the price of the book:600
Book Added
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:3
['Gernimo Stilton', 600] <----TOP
['Secret Seven', 700]
['Famous Five', 500]
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:2
Book Deleted!
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:3
['Secret Seven', 700] <----TOP
['Famous Five', 500]
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:2
Book Deleted!
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:2
Book Deleted!
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:3
Stack is Empty!
1.Add new book(s)
2.Delete a book
3.Display the books
4.EXIT
Enter your choice:4
Exited!