0

is there any way to print each new student without copping and pasting the code like I have done below? Could it be possible to use a loop or something like that?

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
       if self.course == "Computing":
            room = "S227"
       elif self.course == "Art":
            room = "Art Studio 1"
       else:
            room = "Main hall"
       return (room)
    def parientSign(self):
       if self.age > 17:
            print("Parent doesn't need to sign")
       else:
            print("Parent needs to sign")
       return
newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)
print("Student Name: ", newStudent.name)
print("Student Course: ",newStudent.course)
print("Your room number is: ", newStudent.roomNumber())
print("Your Age is: ",newStudent.age)
newStudent.parientSign()
print ("\n--------------\n")
print("Student Name: ", newStudent1.name)
print("Student Course: ",newStudent1.course)
print("Your room number is: ", newStudent1.roomNumber())
print("Your Age is: ",newStudent1.age)
newStudent1.parientSign()
print ("\n--------------\n")
print("Student Name: ", newStudent2.name)
print("Student Course: ",newStudent2.course)
print("Your room number is: ", newStudent2.roomNumber())
print("Your Age is: ",newStudent2.age)
newStudent2.parientSign()
print ("\n--------------\n")
BigSteve
  • 1
  • 3

3 Answers3

1

Credit & as mentioned by DeepSpace in comments

You can try something like this:

class Student:

    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age

    def __str__(self):
        return """Student Name: %s\n
                Student Course: %s\n
                Your room number is: %s\n
                Your Age is: %s\n
                %s""" % (self.name, self.course, self.age, self.roomNumber(), self.parentSign())

    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)

    def parentSign(self):
            return "Parent doesn't need to sign" if self.age > 17 else "Parent needs to sign"

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent)
print(newStudent1)
print(newStudent2)
Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
1
class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age

    def roomNumber(self):
       if self.course == "Computing":
            room = "S227"
       elif self.course == "Art":
            room = "Art Studio 1"
       else:
            room = "Main hall"
       return (room)

    def parientSign(self):
       if self.age > 17:
            print("Parent doesn't need to sign")
       else:
            print("Parent needs to sign")
       return

    def printStudent(self):
        print("Student Name: ", self.name)
        print("Student Course: ", self.course)
        print("Your room number is: ", self.roomNumber())
        print("Your Age is: ", self.age)

studentList = []
studentList.append(Student("Name One", "Computing", 18))
studentList.append(Student("Bob Smart", "Art", 19))
studentList.append(Student("Big Terry", "Computing", 16))

for student in studentList:
    student.printStudent()
    student.parientSign()
    print("------------")

You can create a printer function and create your students inside your list and loop over them to print their properties

ihsancemil
  • 432
  • 5
  • 16
0

There are many ways to solve this problem. The Way I prefer is by using the __repr__ method of the class

Here's an implementation:

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return
    def __repr__(self):
        return "Student({},{},{})".format(self.name, self.course,self.age)

newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

print(newStudent,newStudent1,newStudent2,sep = "\n")

I assume that the use of the __repr__ method is self explanatory. You can basically print the class name and all it's attributes

Another (not recommended) way to do this is by using the list of the the students object.

class Student:
    def __init__(self, name, course, age):
        self.name = name
        self.course = course
        self.age = age
    def roomNumber(self):
        if self.course == "Computing":
            room = "S227"
        elif self.course == "Art":
            room = "Art Studio 1"
        else:
            room = "Main hall"
        return (room)
    def parientSign(self):
        if self.age > 17:
            print("Parent doesn't need to sign")
        else:
            print("Parent needs to sign")
        return


newStudent = Student("Name One", "Computing", 18)
newStudent1 = Student("Bob Smart", "Art", 19)
newStudent2 = Student("Big Terry", "Computing", 16)

l = [newStudent,newStudent1,newStudent2]

for students in l:
    print("Student Name: ", students.name)
    print("Student Course: ",students.course)
    print("Your room number is: ", students.roomNumber())
    print("Your Age is: ",students.age)
    students.parientSign()
aditya rawat
  • 105
  • 8
  • Why would the list not be recommended? – juanpa.arrivillaga Oct 17 '18 at 16:10
  • Because you will have to create a list explicitly that will take memory(though low). Also by using `__repr__` you are not creating anything new as it is part of every python class and is more object oriented and also good practice – aditya rawat Oct 17 '18 at 20:14
  • Don't aggree. What is more object oriented and what is not? List can be use when you need it! Lets imagine that you have to get students from Db, what would you store that students in? Don't be rigid when it comes to coding practices. You may need to extend the idea if extension fits your problem. – ihsancemil Oct 18 '18 at 06:44
  • getting students from database is not same as mere printing to see what values had the object stored. If you are using a database, then sure you would want to store the values in a list to use them later, but for only printing to see what values had been stored in the object's attributes, making a list (or a new function) is more of an unnecessary effort . Just use the dunder methods which are specifically designed for these purposes. But if you are still confused see this https://stackoverflow.com/questions/31824178/what-is-the-significance-of-repr-function-over-normal-function – aditya rawat Oct 18 '18 at 07:13