Q.Shreyas is a programmer, who has recently been given a task to write a user defined function named write_bin() to create a binary file called Cust_file.dat containing customer information - customer number (c_no), name (c_name), quantity (qty), price (price) and amount (amt) of each customer. The function accepts customer number, name, quantity and price. Thereafter, it displays the message 'Quantity less then 10 ... Cannot SAVE', if quantity entered is less than 10. Otherwise the function calculates amount as price*quantity and then writes the record in the form of a list into the binary file. import pickle def write_bin(): bin_file=___________ # Statement 1 while true: c_no=int(input("enter customer number")) C_name=input("enter customer name") qty=int(input("enter qty")) price=int(input("enter price")) if ___________ # Statement 2 print("Quantity less than 10..Cannot SAVE") else: amt=price * qty c_detail=[c_no,c_name,qty,price,amt] ___________ # Statement 4 ___________ # Statement 5 ___________ # Statement 6
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Part (a): Statement 1 = open('Cust_file.dat','wb'), Statement 2 = if qty < 10:, Statement 3 = pickle.dump(c_detail, bin_file), Statement 4 = input(...)+break.
Part (b): Statement 5 = bin_file.close(), Statement 6 = write_bin().
Binary files store Python objects directly as a byte stream. The pickle module handles this serialisation: pickle.dump(obj, file) writes an object, and the file must be opened in a binary mode. Here each customer record is a list [c_no, c_name, qty, price, amt] written to Cust_file.dat.
Part (a)
(i) Statement 1 — opening for binary write. Writing needs mode 'w'; a binary file needs 'b'; together 'wb'. This creates the file (or truncates it if it exists) and returns a file object.
bin_file = open('Cust_file.dat', 'wb') # Statement 1
(ii) Statement 2 — the quantity guard. The message must appear when quantity is below 10, so the condition is a plain comparison.
if qty < 10: # Statement 2
print("Quantity less than 10..Cannot SAVE")
(iii) Statements 3 & 4 — write the record and allow the user to stop. After the record list is built in the else branch, pickle.dump() serialises it into the file. To leave the while True loop, the program asks the user and uses break.
import pickle
def write_bin():
bin_file = open('Cust_file.dat', 'wb') # Statement 1
while True:
c_no = int(input("enter customer number"))
c_name = input("enter customer name")
qty = int(input("enter qty"))
price = int(input("enter price"))
if qty < 10: # Statement 2
print("Quantity less than 10..Cannot SAVE")
else:
amt = price * qty
c_detail = [c_no, c_name, qty, price, amt]
pickle.dump(c_detail, bin_file) # Statement 3
if input("Add more records? (y/n): ").lower() == 'n': # Statement 4
break
bin_file.close() # Statement 5
write_bin() # Statement 6
Expected behaviour: for a record with qty = 5 the program prints
Quantity less than 10..Cannot SAVE
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.