0% found this document useful (0 votes)
22 views14 pages

Programs 11-17

Uploaded by

kstsk2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Programs 11-17

Uploaded by

kstsk2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

BINARY FILE-1

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

1.Perform To Create A Binary File


2.Perform To Display The File
3.Perform To Search The Meaning Of The Word In The File
Enter your choice:1
Enter how many words and their meanings you want:2
Enter A Word:Abandon
Enter Meaning Of The Word:cease to support or look after someone
Enter A Word:Abscond
Enter Meaning Of The Word:leave hurriedly and secretly, typically to avoid detection of or arrest for an
unlawful action such as theft
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 To Search The Meaning Of The Word In The File
Enter your choice:2
{'Abandon': 'cease to support or look after someone'}
{'Abscond': 'leave hurriedly and secretly, typically to avoid detection of or arrest for an unlawful action
such as theft'}
Would you like to continue:(y/n)y
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform To Search The Meaning Of The Word In The File
Enter your choice:3
Enter The Word Whose Meaning Is To Be Searched:Abscond
Meaning of Abscond : leave hurriedly and secretly, typically to avoid detection of or arrest for an
unlawful action such as theft
Would you like to continue:(y/n)n
CSV FILE - 1

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

1.Create A CSV File


2.Display The Content File
3.Search On Basis Of Employee Id
Enter your choice:1
enter the id: 001
enter salary: 100000
enter desegnation: sales
do you want to add more records: y
enter the id: 0112
enter salary: 1000000000000
enter desegnation: tech
do you want to add more records: n
Would you like to continue:(y/n)y
1.Create A CSV File
2.Display The Content File
3.Search On Basis Of Employee Id
Enter your choice:2
id salary designation
1 100000 sales
112 1000000000000 tech
Would you like to continue:(y/n)y
1.Create A CSV File
2.Display The Content File
3.Search On Basis Of Employee Id
Enter your choice:3
enter the employee Id: 112
112 1000000000000 tech
Would you like to continue:(y/n)n
CSV FILE – 2
import csv
def create():
with open('student.csv','w', newline='') as f:
w = csv.writer(f)
a = 'y'
while a=='y':
stuId = int(input('enter the id: '))
name = input('enter name: ')
s1 = int(input('enter subject 1 mark: '))
s2 = int(input('enter subject 2 mark: '))
s3 = int(input('enter subject 3 mark: '))
s4 = int(input('enter subject 4 mark: '))
s5 = int(input('enter subject 5 mark: '))
msum = s1+s2+s3+s4+s5
avg=msum/5
l=[stuId, name, msum, avg]
w.writerow(l)
a = input('do you want to add more records: ')

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

numtog = random.randint(1, 100)


a=0

while True:
try:

guess = int(input("Enter your guess: "))


a += 1

if guess < numtog:


print("Too low! Try again.")
elif guess > numtog:
print("Too high! Try again.")
else:
print("Congratulations! You guessed the number in",a,"attempts.")
break
except ValueError:
print("Please enter a valid number.")

ggame()
OUTPUT:

Welcome to the Random Number Guessing Game!


Think of a number between 1 and 100.
Enter your guess: 7
Too low! Try again.
Enter your guess: 24
Too low! Try again.
Enter your guess: 50
Too high! Try again.
Enter your guess: 40
Too high! Try again.
Enter your guess: 30
Too low! Try again.
Enter your guess: 35
Too high! Try again.
Enter your guess: 33
Congratulations! You guessed the number in 7 attempts.
SIXTEENTH PROGRAM

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!

You might also like