有没有办法打印每个新学生而无需像我在下面所做的那样复制和粘贴代码?是否可以使用循环或类似的东西?
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")
相关分类