Creating objects in a loop in Python [duplicate]

This question already has an answer here:

this is my first post so I am sorry if I get any formatting wrong.

I have searched this before in stackoverflow and it did come up with a few responses but I didn’t really understand as I couldn’t relate to the code. I am trying to practise object oriented programming and what I am trying to do is I have a Student class and I have a menu that I want to loop as long as the user does not input -1. Right now when the user presses 1, I want a new object to be created, but every time that the menu loop loops, I want a new object to be made. What I am thinking could work is adding a counter for a while loop for the mainfunction, and everytime the user inputs “1”, the counter adds 1. THen when creating the object it could look is: student = Studentcounter. I am not really sure and I am sorry if there are many other posts like this but I am a newbie haha.

class Student:
#name = ""      Do i need to put there variables here?
#age = 0
#science = 0
#maths = 0
#english = 0
def __init__(self,name,age,science,maths,english):
    self.name = name
    self.age = age
    self.science = science
    self.maths = maths
    self.english = english
    print(self.name)
    print(self.age)
    print(self.science)
    print(self.maths)
    print(self.english)

def mainFunct():
dictStudent = {}
mainBool = True
while mainBool == True:
    print("WElcome to rafs student grade book a")
    #while True:
        #try:
    menu = int(input("1) view students in specific class 2) Delete Student 3) Highest to lowest grades 4) Change student to a new class 5) add new marks 6) Change"))
            #break
        #except ValueError:
                  #print("Please enter in a number")

    if menu == 1:
        name = input("What is their name")
        age = input("What is their age")
    while True:
        science = int(input("What was their science result out of 100"))
        if science <= 100:
            break
    while True:
        maths = int(input("What was their maths result out of 100"))
        if maths <= 100:
            break
    while True:
        english = int(input("What was their english result out of 100"))
        if english <= 100:
            break
    student = Student(name,age,science,maths,english)




mainFunct()


Source: oop

Leave a Reply