为了账号安全,请及时绑定邮箱和手机立即绑定

有没有更好的方法来打印一个以上的面向对象编程的学生?

有没有更好的方法来打印一个以上的面向对象编程的学生?

慕侠2389804 2021-07-12 16:19:04
有没有办法打印每个新学生而无需像我在下面所做的那样复制和粘贴代码?是否可以使用循环或类似的东西?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")       returnnewStudent = 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")
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信