0% found this document useful (0 votes)
3 views

Binary

Uploaded by

kazashi25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Binary

Uploaded by

kazashi25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Binary File

1. Write a menu driven program to create a list by taking bookno, name, price,
quantity, category from user.
a. Create a binary file storing the contents of the list.
b. Display all. Also calculate Amount which should be calculated by multiplying price
* quantity.
c. Search and display record on Bookno.
d. Search and display record on name.
e. Count all records of a user given category.
f. Delete a records matching the user given record number
g. Edit the nth record. Input n from user

import pickle
def create():
f1=open("bookstore.dat",'wb')
recs=[]
while True:
bookno=int(input('enter book number:'))
name=input('enter name:')
price=float(input("enter price:"))
quantity=int(input("enter quantity:"))
category=input("enter category:")
recs+=[[bookno,name,price,quantity,category]]
ans=input('want to continue adding records?')
if ans in 'Nn':
break
pickle.dump(recs,f1)
f1.close()
def display_all():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
if len(recs)==0:
print("empty file")
for r in recs:
print("book number:", r[0], "name:", r[1], "price:", r[2], "quantity:", r[3],
category:", r[4], "amount:", r[2]* r[3])
def search_display_bookno():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
bno=int(input("enter book number to search with:"))
for r in recs:
if r[0]==bno:
print("book number:", r[0], "name:", r[1], "price:", r[2], "quantity:", r[3],
"category:", r[4], "amount:", r[2]*r[3])
break
else:
print("invalid book number")
def search_display_name():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
nm=input("enter book name to search with:")
for r in recs:
if r[1]==nm:
print("book
number:",r[0],"name:",r[1],"price:",r[2],"quantity:",r[3],"category:",r[4],"amount:",r[ 2]
*r[3])
break
else:
print("invalid book name")
def search_count_category():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
ctr=0
cat=input("enter book category to search with:")
for r in recs:
if r[4]==cat:
ctr+=1
print("the number of books of searched category are:",ctr)
def delete_record():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
rec=[]
bno=int(input("enter book number to search with and delete:"))
for r in recs:
if r[0]!=bno:
rec+=[r]
f1=open("bookstore.dat",'wb')
pickle.dump(rec,f1)
f1.close()
def edit_nth_record():
f1=open("bookstore.dat",'rb')
recs=pickle.load(f1)
f1.close()
cnt=0
rec=[]
n=int(input("enter position of record to edit:"))
if n>len(recs):
print("invalid position")
return
for r in recs:
cnt+=1
if cnt==n:
r[0],r[1],r[2],r[3],r[4]=int(input('enter book number:')),input('enter
name:'),float(input("enter price:")),int(input("enter
quantity:")),input("enter category:")
rec+=[r]
f1=open("bookstore.dat",'wb')
pickle.dump(rec,f1)
f1.close()
while True:
ch=int(input(" 1:create \n 2:display all \n 3:search on book number \n 4:search on
book name \n 5:count on book category \n 6: delete record \n 7:edit nth record \n
enter choice:"))
if ch==1:
create()
elif ch==2:
display_all()
elif ch==3:
search_display_bookno()
elif ch==4:
search_display_name()
elif ch==5:
search_count_category()
elif ch==6:
delete_record()
elif ch==7:
edit_nth_record()
else:
print("invalid choice")
ans=input("want to continue with choosing functions?")
if ans in 'nN':
break
output:
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:1
enter book number:1
enter name:Computer Science
enter price:200
enter quantity:4
enter category:study
want to continue adding records?y
enter book number:2
enter name:Maths
enter price:300
enter quantity:10
enter category:study
want to continue adding records?r
enter book number:3
enter name:Physics
enter price:300
enter quantity:5
enter category:science
want to continue adding records?y
enter book number:4
enter name:English
enter price:200
enter quantity:5
enter category:literature
want to continue adding records?n
want to continue with choosing functions?2
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:2
book number: 1 name: Computer Science price: 200.0 quantity: 4 category: study
amount: 800.0
book number: 2 name: Maths price: 300.0 quantity: 10 category: study amount:
3000.0
book number: 3 name: Physics price: 300.0 quantity: 5 category: science amount:
1500.0
book number: 4 name: English price: 200.0 quantity: 5 category: literature amount:
1000.0
want to continue with choosing functions?3
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:3
enter book number to search with:3
book number: 3 name: Physics price: 300.0 quantity: 5 category: science amount:
1500.0
want to continue with choosing functions?4
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:4
enter book name to search with:Physics
book number: 3 name: Physics price: 300.0 quantity: 5 category: science amount:
1500.0
want to continue with choosing functions?5
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:5
enter book category to search with:study
the number of books of searched category are: 2
want to continue with choosing functions?6
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:6
enter book number to search with and delete:1
want to continue with choosing functions?7
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:7
enter position of record to edit:2
enter book number:2
enter name:Computer
enter price:200
enter quantity:4
enter category:study
want to continue with choosing functions?y
1:create
2:display all
3:search on book number
4:search on book name
5:count on book category
6: delete record
7:edit nth record
enter choice:2
book number: 2 name: Maths price: 300.0 quantity: 10 category: study amount:
3000.0
book number: 2 name: Computer price: 200.0 quantity: 4 category: study amount:
800.0
book number: 4 name: English price: 200.0 quantity: 5 category: literature amount:
1000.0
want to continue with choosing functions?n

2. Write a program to create a binary file to store records of a shop having details
as itemno, Name, QOH (QtyOnhand), Price. Create a menu driven program to:
a. Create above binary file (where record is represented as Nested List)
b. Display all records
c. Display all Records whose Price is more than 500
d. Transfer all records whose QOH <200 to another
file. import pickle
def create():
f1=open("shop.dat",'wb')
recs=[]
while True:
itemno=int(input('enter item number:'))
name=input('enter name:')
price=float(input("enter price:"))
qoh=int(input("enter quantity on hand:"))
recs+=[[itemno,name,qoh,price]]
ans=input('want to continue adding records?')
if ans in 'Nn':
break
pickle.dump(recs,f1)
f1.close()
def display_all():
f1=open("shop.dat",'rb')
recs=pickle.load(f1)
f1.close()
if len(recs)==0:
print("empty file")
for r in recs:
print("item number:",r[0],"name:",r[1],"quantity on hand:",r[2],"price:",r[3])
def display_price():
f1=open("shop.dat",'rb')
recs=pickle.load(f1)
f1.close()
for r in recs:
if r[3]>500:
print("item number:",r[0],"name:",r[1],"price:",r[3],"quantity on hand",r[2])
def transfer():
f1=open("shop.dat",'rb')
recs=pickle.load(f1)
f1.close()
for r in recs:
if r[2]<200:
while True:
ch=int(input(" 1:create \n 2:display all \n 3:search and display on price more than
500 \n 4:transfer records with qoh<200 to another file \n enter choice:"))
if ch==1:
create()
elif ch==2:
display_all()
elif ch==3:
display_price()
elif ch==4:
transfer()
else:
print("invalid choice")
ans=input("want to continue with choosing functions?")
if ans in 'nN':
break
output:
1:create
2:display all
3:search and display on price more than 500
4:transfer records with qoh<200 to another file
enter choice:1
enter item number:1
enter name:pen
enter price:10
enter quantity on hand:100
want to continue adding records?l
enter item number:2
enter name:stationary
box enter price:550
enter quantity on hand:300
want to continue adding records?1
enter item number:3
enter name:pencil
enter price:20
enter quantity on hand:200
want to continue adding records?n
want to continue with choosing functions?2
1:create
2:display all
3:search and display on price more than 500
4:transfer records with qoh<200 to another file
enter choice:2
item number: 1 name: pen quantity on hand: 100 price: 10.0
item number: 2 name: stationary box quantity on hand: 300 price: 550.0
item number: 3 name: pencil quantity on hand: 200 price: 20.0
want to continue with choosing functions?3
1:create
2:display all
3:search and display on price more than 500
4:transfer records with qoh<200 to another file
enter choice:3
item number: 2 name: stationary box price: 550.0 quantity on hand 300
want to continue with choosing functions?d
enter choice:4
[[1, 'pen', 100, 10.0]]
want to continue with choosing functions?n

3. Write a program to create a binary file account.dat to store bank account records. Each
record is represented as dictionary with Accno as the key and
name,type_of_account(Saving, Fixed, Recurring) and balance as fields. Create a menu driven
program to:
a. Create above binary file by taking as many records from the user.
b. Display and count the number of records present in the file
c. Search and edit the record of a user given Accno
d. Search and display all Records based on user given name
e. Count all Records having balance > 500000
f. Count all records matching with a user given Type of
account. import pickle
def create():
f1=open("account.dat",'wb')
rdict={}
while True:
accno=int(input('enter account number:'))
name=input('enter name:')
toa=input("enter type of account (savings/fixed/recurring):")
balance=float(input("enter balance:"))
rdict[accno]=[name,toa,balance]
ans=input('want to continue adding records?')
if ans in 'Nn':
break
pickle.dump(rdict,f1)
f1.close()
def display_all():
f1=open("account.dat",'rb')
rdict=pickle.load(f1)
f1.close()
ctr=0
for r in rdict:
ctr+=1
print("account number:",r,"name:", rdict[r][0],"type of
account:",rdict[r][1],"balance:",rdict[r][2])
print("total number of records are:",ctr)

def search_accountno():
f1=open("account.dat",'rb')
rdict=pickle.load(f1)
f1.close()
recdict={}
fd=0
ano=int(input("enter account number to search with:"))
for r in rdict:
if r==ano:
name=input('enter name:')

toa=input("enter type of account (savings/fixed/recurring):")


bal=float(input("enter balance:"))
recdict[r]=[name,toa,bal]
recdict[r]=rdict[r]
fd=1
if
fd==0:
print("invalid account number")
f1=open("account.dat",'wb')
pickle.dump(recdict,f1)
f1.close()
def search_name():
f1=open("account.dat",'rb')
rdict=pickle.load(f1)
f1.close()
fd=0
nm=input("enter name to search with:")
for r in rdict:
if rdict[r][0]==nm:
print("account number:",r,"name:",rdict[r][0],"type of
account:",rdict[r][1],"balance:",rdict[r][2])
fd=1
if fd==0:
print("no records of such name")
def count_records():
f1=open("account.dat",'rb')
rdict=pickle.load(f1)
ctr=0
f1.close()
for r in
rdict:
if rdict[r][2]>500000:
ctr+=1
print("no_of_records_with_balance_greater_than_500000_are",ctr)

def account():
f1=open("account.dat",'rb')
rdict=pickle.load(f1)
ctr=0
f1.close()
ty=input("enter type of account (savings/fixed/recurring):")
for r in rdict:
if rdict[r][1]==ty:
ctr+=1
print("no of records with matching type of account",ctr)
while True:
ch=int(input(" 1:create \n 2:display_all_records_and_count \n 3:search and edit on
account number \n 4:search and display on name \n 5:count records with balance greater
than 500000 \n 6: count records matching with user given type of account \n enter
choice:"))
if ch==1:
create()
elif ch==2:
display_all()
elif ch==3:
search_accountno()
elif ch==4:
search_name()
elif ch==5:
count_records()
elif ch==6:
account()
else:

print("invalid choice")
ans=input("want to continue with choosing functions?")
if ans in 'nN':
break

output:
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000
6: count records matching with user given type of
account enter choice:1
enter account number:10024
enter name:lala
enter type of account (savings/fixed/recurring):savings
enter balance:1000000
want to continue adding records?
2 enter account number:10025
enter name:lily
enter type of account (savings/fixed/recurring):fixed
enter balance:50000
want to continue adding records?
r enter account number:10026
enter name:1
enter type of account (savings/fixed/recurring):recurring
enter balance:3000000
want to continue adding records?n
want to continue with choosing functions?2
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000
6: count records matching with user given type of
account enter choice:2
account number: 10024 name: lala type of account: savings balance: 1000000.0
account number: 10025 name: lily type of account: fixed balance: 50000.0
account number: 10026 name: 1 type of account: recurring balance: 3000000.0
total number of records are: 3
want to continue with choosing functions?3
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000
6: count records matching with user given type of
account enter choice:3
enter account number to search with:10026
enter name:hii
enter type of account (savings/fixed/recurring):recurring
enter balance:4000000
want to continue with choosing functions?4
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000
6: count records matching with user given type of
account enter choice:4
enter name to search with:lala
account number: 10024 name: lala type of account: savings balance: 1000000.0
want to continue with choosing functions?5
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000
6: count records matching with user given type of
account enter choice:5
no_of_records_with_balance_greater_than_500000_are 2
want to continue with choosing functions?6
1:create
2:display_all_records_and_count
3:search and edit on account number
4:search and display on name
5:count records with balance greater than 500000

6: count records matching with user given type of


account enter choice:6
enter type of account (savings/fixed/recurring):savings
no of records with matching type of account 1
want to continue with choosing functions?n

You might also like