| 1. |
A global bank manages its customer in a binary data file “Bank.DAT” with the following fields:• Account number • Account holder’s name • Account balance Give function definitions to do the following: a) Write a function to accept the data fields from the user and write to file “Bank.DAT”. b) Write a function to modify account holder details according to their account number and display the updation. |
Answer» LANGUAGE:Python Program:a) def createFile(): import pickle f=open('Bank.dat','wb') record=[] while True: accno=input("Enter account number:") holder=input(f"Enter {accno} holder's name: ") balance=float(input(f"Enter {accno}'s balance: ")) choice=input("Wanna enter more (Y/N)?: ") record.append([accno, holder, balance]) if choice=='N': pickle.dump(record,f) print(len(record),'record(s) inserted') f.close() b) def UPDATE(): import pickle f=open('Bank.dat','rb+') read=pickle.load(f) found=0 acc=input('Enter account number to update: ') for i in read: if i[0]==acc: print("Make the following choice for updation: \nHolder's name\t1 \nAccount Balance\t2 \nCancel updation\t3") choice=int(input("Enter your choice: ")) found+=1 if choice==1: i[1]=input('Enter new Holder\'s name: ') break ELIF choice==2: i[2]=int(input("Enter new Balance:")) break elif choice==3: print("ABORTING updation.") else: print("Invalid choice") break else: found+=0 if found==1: f.seek(0) pickle.dump(read,f) print("Done.") else: print("Record not found.") f.close() Explanation:
|
|