0

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()
mre12345
  • 1,087
  • 4
  • 16
  • 23
  • 1
    The short answer here is that you _don't_ want to do this, you want to create a list `students = []`, and just `append` to it. See [here](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) and [here](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) if the linked duplicate answer doesn't explain it well enough. (Also try looking at the dozens of linked and related answers from the linked duplicate, some of which may be more obviously similar to your case.) – abarnert May 24 '15 at 23:28
  • The duplicate flag points you to the answer to the question how to dynamically create variables (and why you shouldn't). But a more direct answer to your question can be found in [this question](http://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-in-python-via-a-while-loop) or [this question](http://stackoverflow.com/questions/21598872/how-to-create-multiple-class-objects-with-a-loop-in-python) about how to dynamically create class instances - and why you shouldn't – LinkBerest May 25 '15 at 00:33

0 Answers0