हिंदी

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 - Computer Science (Python)

Advertisements
Advertisements

प्रश्न

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:

  1. Create()− to input data for passengers and write it in the binary file PASSENGERS.DAT.
  2. SearchDestn(D)- to read contents from the file PASSENGERS.DAT and display the details of those Passengers whose DESTN matches with the value of D.
  3. UpdateFare()- to increase the fare of all passengers by 5% ad rewrite the updated records into the file PASSENGERS.DAT.
कोड लेखन
Advertisements

उत्तर

import pickle
import os
def Create():
    pnr=input("Enter passenger number:")
    pname=input("Enter passenger name:")
    brdstn=input("Enter boarding station:")
    destn=input("Enter destination station:")
    fare=float(input("Enter fare:"))
    prec=[pnr,pname,brdstn,destn,fare]
    f=open("passengers.dat","ab")
    pickle.dump(prec,f)
    f.close()
def SearchDestn(D)
    f=open("passengers.dat","rb")
    try:
     while True:
          prec=pickle.load(f)
      if D==prec[3]:
        print(prec)
        found=True
except:
  if found=False:
    print("Record not found")
  f.close()
def UpdateFare():
    f1=open("passengers.dat","rb")
    f2=open("temp.dat","wb")
try:
   while True:
      prec=pickle.load(f)
     fr=int(prec[4])
     fr=fr+fr*0.05
     prec[4]=str(fr)
    pickle.dump(prec,f2)
except:
    f1.close()
    f2.close()
    rename("temp.dat","passengers.dat")
    remove("temp.dat")
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×