Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, November 24, 2020

Python program to create a binary file with roll number, name and marks. Input a roll number and update the marks by G Krishna Chauhan

Source Code


import pickle

def  Writerecord(sroll,sname,sperc,sremark):

    with open ('StudentRecord.dat','ab') as Myfile:

        srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,

                 "SREMARKS":sremark}        

        pickle.dump(srecord,Myfile)

       

def Readrecord():

    with open ('StudentRecord.dat','rb') as Myfile:

        print("\n-------DISPALY STUDENTS DETAILS--------")

        print("\nRoll No.",' ','Name','\t',end='')

        print('Percetage',' ','Remarks')

        while True:

           try:

               rec=pickle.load(Myfile)

               print(' ',rec['SROLL'],'\t  ' ,rec['SNAME'],'\t ',end='')

               print(rec['SPERC'],'\t   ',rec['SREMARKS'])

           except EOFError:

                break

def Input():

    n=int(input("How many records you want to create :"))

    for ctr in range(n):

        sroll=int(input("Enter Roll No: "))

        sname=input("Enter Name: ")

        sperc=float(input("Enter Percentage: "))

        sremark=input("Enter Remark: ")

        Writerecord(sroll,sname,sperc,sremark)

        


def Modify(roll):

    with open ('StudentRecord.dat','rb') as Myfile:

        newRecord=[]

        while True:

           try:

               rec=pickle.load(Myfile)

               newRecord.append(rec)

           except EOFError:

                break

    found=1       

    for i in range(len(newRecord)):

        if newRecord[i]['SROLL']==roll:

            name=input("Enter Name: ")

            perc=float(input("Enter Percentage: "))

            remark=input("Enter Remark: ")


            newRecord[i]['SNAME']=name

            newRecord[i]['SPERC']=perc

            newRecord[i]['SREMARKS']=remark

            found =1

        else:

            found=0


    if found==0:


        print("Record not found")

    with open ('StudentRecord.dat','wb') as Myfile:

         for j in newRecord:

             pickle.dump(j,Myfile)



def main():

   

    while True:

        print('\nYour Choices are: ')

        print('1.Insert Records')

        print('2.Dispaly Records') 

        print('3.Update Records')

        print('0.Exit (Enter 0 to exit)')

        ch=int(input('Enter Your Choice: '))

        if ch==1:

            Input()

        elif ch==2:

            Readrecord()

        elif ch==3:

            r =int(input("Enter a Rollno to be update: "))

            Modify(r)

        else:

            break

main()





OUTPUT


Your Choices are: 
1.Insert Records
2.Dispaly Records
3.Update Records
0.Exit (Enter 0 to exit)
Enter Your Choice: 1
How many records you want to create :1
Enter Roll No: 01
Enter Name: abc
Enter Percentage: 89
Enter Remark: GOOD

Your Choices are: 
1.Insert Records
2.Dispaly Records
3.Update Records
0.Exit (Enter 0 to exit)
Enter Your Choice: 2

-------DISPALY STUDENTS DETAILS--------

Roll No.   Name Percetage   Remarks
  1    abc 89.0     GOOD

Your Choices are: 
1.Insert Records
2.Dispaly Records
3.Update Records
0.Exit (Enter 0 to exit)

Enter Your Choice: 3
Enter a Rollno to be update: 01
Enter Name: XYZ
Enter Percentage: 89
Enter Remark: GOOD

Your Choices are: 
1.Insert Records
2.Dispaly Records
3.Update Records
0.Exit (Enter 0 to exit)
Enter Your Choice: 2

-------DISPALY STUDENTS DETAILS--------

Roll No.   Name  Percetage   Remarks
  1         XYZ   89.0      GOOD

No comments:

Post a Comment