CBSE · 083Class XII · 2025Section E2 marks
Question 36i
4 March 2025 · Computer Science (083)
A file, PASSENGERS.DAT, stores the records of passengers using the following structure: [PNR, PName, BRDSTN, DESTN, FARE], where: PNR – Passenger Number (string type) PName – Passenger Name (string type) BRDSTN – Boarding Station Name (string type) DESTN – Destination Station Name (string type) FARE – Fare amount for the journey (float type) Write user defined functions in Python for the following tasks : (i) Create() – to input data for passengers and write it in the binary file PASSENGERS.DAT.
Answer
python
import pickle
def Create():
F=open("PASSENGERS.DAT", "wb")
PNR=input("PNR No:")
PName=input("Name: ")
BRDSTN=input("Boarding at: ")
DESTN=input("Destination: ")
FARE=float(input("Fare: "))
Rec=[PNR,PName,BRDSTN,DESTN,FARE]
pickle.dump(Rec,F)
F.close()Explanation
Opens binary file in write mode, accepts inputs, dumps list object using pickle.