This code is a Simple Student Management System in which I want it to receive inputs from the user and display according to the call. I have created a class Student and initialized the variables. I created a method accept(self) for receiving the user inputs and appending the details to a list of dictionaries. But I am stuck on how I can initialize the inputs that I have received. How can I call the methods outside of class Student since I am unable to create an object for class Student? I am a beginner in Python. Having a hard time understanding OOP. Please help me! Here is the code I wrote:
class Student:
student_details = []
def __init__(self, name, rollno, mark1, mark2):
self.name = name
self.rollno = rollno
self.mark1 = mark1
self.mark2 = mark2
def accept(self):
no_of_entries = int(input("Enter the number of entries: "))
i = 1
while i <= no_of_entries:
print(f"Student {i}")
name = input("Name: ")
rollno = int(input("Roll: "))
mark1 = int(input("Mark 1: "))
mark2 = int(input("Mark 2: "))
Student(name, rollno, mark1, mark2).student_details.append({
"Name": self.name,
"Rollno": self.rollno,
"Mark1": self.mark1,
"Mark2": self.mark2,
})
print()
i = 1
print(Student.student_details)
student1 = Student()
proceed = True
while proceed:
print("STUDENT MANAGEMENT SYSTEM".center(50, '-'))
print()
print("* Accept Student entries (1): ")
print("* Search Student entries (2): ")
print("* Update Student entries (3): ")
print("* Delete Student entries (4): ")
print("* Display Student entries (5): ")
choice = int(input("Enter your choice: "))
if choice == 1:
I get the error:
Student.__init__() missing 4 required positional arguments: 'name', 'rollno', 'mark1', and 'mark2'
I don't know what to do. Even this is the problem I am facing with OOP in Python. If anyone can help me with this than I will be grateful
CodePudding user response:
EDIT: The callback comes from the student1 = Student() delete the statement and declare the accept method as a static method. Then you can deklare a student in the method and can appent the student.
Your mistake should be here:
Student(name, rollno, mark1, mark2).student_details.append({
"Name": self.name,
"Rollno": self.rollno,
"Mark1": self.mark1,
"Mark2": self.mark2,
})
You try to initialise a object and then you try to use it as a dict.
st1 = Student(name, rollno, mark1, mark2)
Student.student_details.append(st1)
Also you can use a classattribut in a object but the changes will only be present in the object. It wouldnt be in all object.
It would be best make the method accept in a function or you will end up with many object with the same values. If you want to leave it as a method you can change the code to:
Student.student_details.append(self)
CodePudding user response:
By default methods defined in a Python class are instance methods, so they need to be called on an already-existing object instance. If you want to create a custom constructor, the method should be probably marked as a class method by using @classmethod
decorator, so it can be called on the class itself.
class Student:
student_details = []
def __init__(self, name, rollno, mark1, mark2):
self.name = name
self.rollno = rollno
self.mark1 = mark1
self.mark2 = mark2
@classmethod
def accept(cls):
no_of_entries = int(input("Enter the number of entries: "))
for i in range(no_of_entries):
print(f"Student {i}")
name = input("Name: ")
rollno = int(input("Roll: "))
mark1 = int(input("Mark 1: "))
mark2 = int(input("Mark 2: "))
student = cls(name, rollno, mark1, mark2)
cls.student_details.append(student)
print(cls.student_details)
Student.accept()
CodePudding user response:
student1 = Student()
you need to pass 4 parameters here
def __init__(self, name, rollno, mark1, mark2):
So you should have
student1 = Student('name', 'rollno', 'mark1', 'mark2')
or something
CodePudding user response:
You forgot to put arguments when you call that class. For example:
student1 = Student(name='a', rollno='0', mark1='1', mark2='2')
I'm not sure what your objective is. If you just want to input student information into a list, maybe the code below can give you an idea.
class Student:
student_details = []
def __init__(self, name, rollno, mark1, mark2):
self.name = name
self.rollno = rollno
self.mark1 = mark1
self.mark2 = mark2
student_list = []
print("STUDENT MANAGEMENT SYSTEM".center(50, '-'))
print()
print("* Accept Student entries (1): ")
print("* Search Student entries (2): ")
print("* Update Student entries (3): ")
print("* Delete Student entries (4): ")
print("* Display Student entries (5): ")
choice = int(input("Enter your choice: "))
if choice == 1:
print("You have selected (1) - Accept Student entries")
no_of_entries = int(input("Enter the number of entries: "))
for i in range(0, no_of_entries):
print("Input student no", i 1, "information.")
student = Student(name=input("Name: "), rollno=input("Roll: "), mark1=input("Mark 1: "), mark2=input("Mark 2: "))
student_list.append(student)
elif choice == 2:
pass
elif choice == 3:
pass
elif choice == 4:
pass
elif choice == 5:
pass
else:
print("Invalid choice.")