Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.7 kB
4
Indexable
Never
try: #Start of try and exception
    print()
    class StudentRecord: #Start of declaring Student Record Structure
        def __init__(self, name, marks, school):
            self.name = name
            self.marks = marks
            self.school = school
    
    stack = ['' for i in range(2)] #Declaring length of array
    stackfull = len(stack)
    basePointer = 0
    topPointer = -1
    
    def push(item): #Start of function
        global topPointer #Declaring pointer globally for assigning
        if topPointer < stackfull - 1:
            topPointer = topPointer + 1
            stack[topPointer] = item
        else:
            print("Stack is full, cannot push more than stack length")
    
    yesno = int(input("Would you like to push any records into the stack?\n1 for Yes\n2 for No\nEnter here: ")) #Asking if any records need to be pushed
    if yesno == 1: #Start of iteration
        print()
        lenitems = int(input("How many records do you want to push?: ")) #Asking how many records need to be pushed
        while lenitems > 0: #Start of while iteration to input user data
            print()
            name = input("Enter name: ")
            marks = input("Enter marks: ")
            school = input("Enter school: ")
            item = StudentRecord(name, marks, school) #Data assignment
            push(item) #Calling function
            lenitems = lenitems - 1
    
    if yesno == 1: #Start of iteration to re-print pushed data
        print()
        print("Stack after item push --->")
        for item in stack:
            if item != '':
                print()
                print("Name:", item.name)
                print("Marks:", item.marks)
                print("School:", item.school)
                print()
    
    def pop(): #Start of function
        global topPointer, basePointer, item #Declaring pointer globally for assigning
        if topPointer == basePointer - 1:
            print("Stack is empty, cannot pop more records")
        else:
            item = stack[topPointer]
            stack[topPointer] = ''
            topPointer = topPointer - 1
            print()
            print("Popped record --->")
            print()
            print("Name:", item.name)
            print("Marks:", item.marks)
            print("School:", item.school)
    
    yesno2 = int(input("Would you like to pop any records in the stack?\n1 for Yes\n2 for No\nEnter here: ")) #Asking user whether any data needs to be popped or not
    if yesno2 == 1: #Iteration
        lenitems2 = int(input("How many records do you want to pop?: "))
        for i in range(lenitems2):
            pop()
    
    if yesno2 == 1: #NO MORE COMMENTS BELOW PLS IGNORE
        print()
        print("Stack after record pop --->")
        for item in stack:
            if item != '':
                print()
                print("Name:", item.name)
                print("Marks:", item.marks)
                print("School:", item.school)
                print()
    
    with open("E:\\EvidenceRecordFolder\studentdata.txt", "w") as file:
        for item in stack:
            if item != '':
                file.write("Name: " + item.name + "\n")
                file.write("Marks: " + item.marks + "\n")
                file.write("School: " + item.school + "\n")
                file.write("\n")
                
    f = open("E:\\EvidenceRecordFolder\studentdata.txt", "r")
    print()
    print("The final record/s written in the final is printed below")
    print()
    print(f.read())
    
except Exception as err:
    print("An error occurred:", str(err))