Support our Blind Hand Charity by donating at 9818291723. Your contribution has the potential to bring about significant positive change for humanity.

Search This Blog

Sunday, July 7, 2024

Student Reocrd Keeper(CSV)

 import csv as c


def add(fn):

    try:

        with open(fn,'w') as f:

            w=c.writer(f)

            w.writerow(["Name","Age","Roll No.","Father's Name","Mother's Name","Subject 1","Subject 2","Subject 3","Subject 4","Subject 5","Percentage","Grade","Status"])

            n=input("Enter name = ")

            rn=int(input("Enter the roll number = "))

            ag=int(input("Enter the age = "))

            f=input("Enter father's name = ")

            m=input("Enter mother's name = ")

            m1=int(input("Enter marks of first subject = "))

            m2=int(input("Enter marks of second subject = "))

            m3=int(input("Enter marks of third subject = "))

            m4=int(input("Enter marks of fourth subject = "))

            m5=int(input("Enter marks of fifth subject = "))

            p=(m1+m2+m3+m4+m5)/5

            if 90<p<=100:

                g='A1'

            elif 80<p<=90:

                g='A2'

            elif 70<p<=80:

                g='B1'

            elif 70<p<=80:

                g='B2'

            elif 60<p<=70:

                g='C1'

            elif 50<p<=60:

                g='C2'

            elif 40<p<=50:

                g='D1'

            elif 30<p<=40:

                g='D2'

            elif p<=30:

                g='E'

            if p>=33:

                s="PASS"

            else:

                s="FAIL"

            w.writerow([n,ag,rn,f,m,m1,m2,m3,m4,m5,p,g,s])

    except:

        FileNotFoundError

        with open(fn,'a') as f:

            w=c.writer(f)

            n=input("Enter name = ")

            rn=int(input("Enter the roll number = "))

            ag=int(input("Enter the age = "))

            f=input("Enter father's name = ")

            m=input("Enter mother's name = ")

            m1=int(input("Enter marks of first subject = "))

            m2=int(input("Enter marks of second subject = "))

            m3=int(input("Enter marks of third subject = "))

            m4=int(input("Enter marks of fourth subject = "))

            m5=int(input("Enter marks of fifth subject = "))

            p=(m1+m2+m3+m4+m5)/5

            if 90<p<=100:

                g='A1'

            elif 80<p<=90:

                g='A2'

            elif 70<p<=80:

                g='B1'

            elif 70<p<=80:

                g='B2'

            elif 60<p<=70:

                g='C1'

            elif 50<p<=60:

                g='C2'

            elif 40<p<=50:

                g='D1'

            elif 30<p<=40:

                g='D2'

            elif p<=30:

                g='E'

            if p>=33:

                s="PASS"

            else:

                s="FAIL"

            w.writerow([n,ag,rn,f,m,m1,m2,m3,m4,m5,p,g,s])


def disp(fn):

    with open(fn,'r') as f:

        r=c.reader(f)

        l=[]

        for i in r:

            l.append(i)

        if len(l)>=3:

            print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

            print("Student Name\tAge\tRoll no. Father name\t Mother name\tSub1\tSub2\tSub3\tSub4\tSub5\tPercentage\tGrade")

            print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

            for i in range(2,len(l),2):

                print(l[i][0],'\t',l[i][1],'\t',l[i][2],'\t',l[i][3],'\t',l[i][4],'\t',l[i][5],'\t',l[i][6],'\t',l[i][7],'\t',l[i][8],'\t',l[i][9],'\t',l[i][10],'\t\t',l[i][11],'\t',l[i][12])

                print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

        else:

            print("File is Empty")


def search(fn,ro):

    with open(fn,'r') as f:

        r=c.reader(f)

        l=[]

        for i in r:

            l.append(i)

        l.pop(0)

        l.pop(0)

        co=-1

        for i in range(0,len(l),2):

            if int(l[i][2])==ro:

                co=i

        if co==-1:

            print("\nRecord not Found\n")

        else:

            print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

            print("Student Name\tAge\tRoll no. Father name\t Mother name\tSub1\tSub2\tSub3\tSub4\tSub5\tPercentage\tGrade")

            print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

            print(l[i][0],'\t',l[i][1],'\t',l[i][2],'\t',l[i][3],'\t',l[i][4],'\t',l[i][5],'\t',l[i][6],'\t',l[i][7],'\t',l[i][8],'\t',l[i][9],'\t',l[i][10],'\t\t',l[i][11],'\t',l[i][12])

            print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")


def modify(fn,ro):

    with open(fn,'r') as f:

        r=c.reader(f)

        l=[]

        for i in r:

            l.append(i)

        l.pop(0)

        l.pop(0)

        for i in range(0,len(l),2):

            if int(l[i][2])==ro:

                print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

                print("Student Name\tAge\tRoll no. Father name\t Mother name\tSub1\tSub2\tSub3\tSub4\tSub5\tPercentage\tGrade")

                print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")

                print(l[i][0],'\t',l[i][1],'\t',l[i][2],'\t',l[i][3],'\t',l[i][4],'\t',l[i][5],'\t',l[i][6],'\t',l[i][7],'\t',l[i][8],'\t',l[i][9],'\t',l[i][10],'\t\t',l[i][11],'\t',l[i][12])

                print("──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")                

        co=-1

        while True:

            print("""What do you want to modify:

[1]. Student name

[2]. Age

[3]. Roll no.

[4]. Father name

[5]. Mother name

[6]. Subject 1

[7]. Subject 2

[8]. Subject 3

[9]. Subject 4

[10]. Subject 5""")

            n=int(input("Enter the data number = "))

            if n==1 or n==3 or n==4:

                b=input("Enter data = ")

                break

            elif n==2 or n==5 or n==6 or n==7 or n==8 or n==9 or n==10:

                b=int(input("Enter data = "))

                break

            else:

                print('\nInvalid operation entered\n')

        for i in range(0,len(l),2):

            if int(l[i][2])==ro:

                co=i

                l[i][n-1]=str(b)

        p=(int(l[co][5])+int(l[co][9])+int(l[co][6])+int(l[co][7])+int(l[co][8]))/5

        l[co][10]=p

        if 90<p<=100:

                g='A1'

        elif 80<p<=90:

            g='A2'

        elif 70<p<=80:

            g='B1'

        elif 70<p<=80:

            g='B2'

        elif 60<p<=70:

            g='C1'

        elif 50<p<=60:

            g='C2'

        elif 40<p<=50:

            g='D1'

        elif 30<p<=40:

            g='D2'

        elif p<=30:

            g='E'

        if p>=33:

            s="PASS"

        else:

            s="FAIL"

        l[co][11]=g

        l[co][12]=s

        if co==-1:

            print("Record not Found")

        else:

            with open(fn,'w') as f:

                w=c.writer(f)

                w.writerow(["Name","Age","Father's Name","Mother's Name","Subject 1","Subject 2","Subject 3","Subject 4","Subject 5","Percentage","Grade","Status"])

                for i in range(0,len(l),2):

                    w.writerow(l[i])

            print("\nRecord Modified successfully\n")


def delete(fn,ro):

    with open(fn,'r') as f:

        r=c.reader(f)

        l=[]

        for i in r:

            l.append(i)

        l.pop(0)

        l.pop(0)

        co=-1

        for i in range(0,len(l),2):

            if int(l[i][2])==ro:

                co=i

        if co==-1:

            print("Record not Found")

        else:

            l.pop(co)

            l.pop(co)

            with open(fn,'w') as f:

                w=c.writer(f)

                w.writerow(["Name","Age","Father's Name","Mother's Name","Subject 1","Subject 2","Subject 3","Subject 4","Subject 5","Percentage","Grade","Status"])

                for i in range(0,len(l),2):

                    w.writerow(l[i])

            print("\nRecord Deleted successfully\n")


while True:

    print("""Choose the operation to perform:

[1]. Add a Record

[2]. Display All Record

[3]. Search a Record

[4]. Modify a Record

[5]. Delete a Record

[6]. Exit""")

    o=int(input("Enter the operation number = "))

    if o==6:

        break

    elif o==1:

        add('sturec.csv')

        print("\nRecord added Successfully\n")

    elif o==2:

        disp('sturec.csv')

    elif o==3:

        r=int(input("Enter the roll number = "))

        search('sturec.csv',r)

    elif o==4:

        r=int(input("Enter the roll number = "))

        modify('sturec.csv',r)

    elif o==5:

        r=int(input("Enter the roll number = "))

        delete('sturec.csv',r)

    else:

        print("\nInvalild operation entered\n")