A binary file “Bakery.dat” has structure [ItemNo,NamePdt,Quantity,Price].
i. Write a user defined function CreatePdt() to input data for a record and add to Bakery.dat .
ii. Write a function CountItem(ItemNo) in Python which accepts the Item Number as parameter and count and
return number of items by the given ItemNo are stored in the binary file “Bakery.dat”
Answers
Answer:
1)
def File():
import pickle as p
with open("Bakery.dat","wb") as r:
x=int(input("How many values you want to input="))
y=[]
for i in range(x):
yy=[]
w=int(input("Enter ItemNo="))
ww=input("Enter NamePdt=")
www=input("Enter Quantity=")
wwww=int(input("Enter Price="))
yy.append(w)
yy.append(ww)
yy.append(www)
yy.append(wwww)
y.append(yy)
print()
p.dump(y,r)
File()
2)
def CountItem(ItemNo):
import pickle as p
with open("Bakery.dat","rb") as r:
y=p.load(r)
print(y)
c=0
for i in y:
if i[0]==ItemNo:
c=c+1
print("Nimber of students with stream ",Stream,"are",c)
x=input("Enter item number to search=")
CountItem(x)
Explanation: