class student:
def __init__(self,name,sub,mark):
self.name=name
self.sub=sub
self.mark=mark
def on_kv_board(self):
if (self.mark>=450):
return True
else:`enter code here`
return False
students=[]
num_student=int(input("Enter the number of students appeared:"))
for entries in range(0,num_student):
name=input("Student name:")
sub=input("Subject in which "+name+" got the highest mark:")
mark=int(input("Total marks obtained/500:"))
if student(name,sub,mark).on_kv_board() is True:
a=(student(name,sub,mark))
students.append(a)
print(students)
每当我用两个输入来打印students []列表时,它就会给我一个输出[student.student对象为0x03784790,Student.student对象为0x037D61B0]
答案 0 :(得分:0)
似乎您需要实现一种方法__repr__
:
class student:
def __init__(self,name,sub,mark):
self.name=name
self.sub=sub
self.mark=mark
def __repr__(self):
return f"(Student:[{self.name}, {self.sub}, {self.mark}]) "
def on_kv_board(self):
if (self.mark>=450):
return True
else:
return False
students=[]
num_student=int(input("Enter the number of students appeared:"))
for entries in range(0,num_student):
name=input("Student name:")
sub=input("Subject in which "+name+" got the highest mark:")
mark=int(input("Total marks obtained/500:"))
if student(name,sub,mark).on_kv_board() is True:
a=(student(name,sub,mark))
students.append(a)
print(students)
输出:
Enter the number of students appeared:1
Student name:123
Subject in which 123 got the highest mark:456
Total marks obtained/500:789
[(Student:[123, 456, 789]) ]
__repr__
是可以格式化类输出的函数。