【发布时间】:2021-06-12 19:22:00
【问题描述】:
我需要编写下面的 str 方法来返回一个字符串而不是打印一个字符串。
def __str__(self):
"""Returns the string representation of the student."""
avg = sum(self.scores) / len(self.scores)
print("Name: " + str(self.name))
print("Score 1: " + str(self.scores[0]))
print("Score 2: " + str(self.scores[1]))
print("Score 3: " + str(self.scores[2]))
print("High: " + str(int(max(self.scores))))
print("Average: %.2f\n" % avg)
【问题讨论】:
-
您应该尝试通过替换多个打印,通过返回 (str1 +'\n' +str2+'\n' +...) ...尝试告诉我们您是否设法做到了。