@属性怎么写才正确?
How to write @property correctly?
很遗憾,我不能给你问题的情况,但我可以简单描述一下
我们需要创建两个 classes:
- 第一个 class 创建一张试卷。老师写下科目名称、试题正确答案和成功完成的最低百分比。
- 二年级是学生的考试
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self, tests_taken = {}):
self._tests_taken = tests_taken
@property
def tests_taken(self):
if self._tests_taken == {}:
self._tests_taken = "No tests taken"
return self._tests_taken
else:
return self._tests_taken
def take_test(self, testpaper, answers):
self._tests_taken = {}
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
return self._tests_taken
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
return self._tests_taken
这是我需要检查的内容:
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
这是我的输出:
No tests taken
{'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%
这是正确的输出:
No tests taken
{'Maths': 'Failed! (20%)', 'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%
我必须改变什么?
删除行,重置._tests_taken
变量应该更好:)
def take_test(self, testpaper, answers):
self._tests_taken = {} # this row resets the object._tests_taken
pass_mark = 0
你不必重新初始化那个字典,因为在这里:
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
无论如何,您添加(如果再次使用则替换)正确的值。
也将您的 属性 更改为:
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
完整代码:
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self, tests_taken=None):
if tests_taken is None:
tests_taken = {}
self._tests_taken = tests_taken
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
def take_test(self, testpaper, answers):
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
return self._tests_taken
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
return self._tests_taken
if __name__ == '__main__':
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
只需将测试更改为如下所示:
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
并将 __init__
更改为如下所示:
def __init__(self, ):
self._tests_taken = {}
完整代码是这样的:
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self):
self._tests_taken = {}
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
def take_test(self, testpaper, answers):
self._tests_taken == {}
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
输出:
No tests taken
{'Maths': 'Failed! (20%)', 'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%
很遗憾,我不能给你问题的情况,但我可以简单描述一下 我们需要创建两个 classes:
- 第一个 class 创建一张试卷。老师写下科目名称、试题正确答案和成功完成的最低百分比。
- 二年级是学生的考试
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self, tests_taken = {}):
self._tests_taken = tests_taken
@property
def tests_taken(self):
if self._tests_taken == {}:
self._tests_taken = "No tests taken"
return self._tests_taken
else:
return self._tests_taken
def take_test(self, testpaper, answers):
self._tests_taken = {}
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
return self._tests_taken
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
return self._tests_taken
这是我需要检查的内容:
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
这是我的输出:
No tests taken
{'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%
这是正确的输出:
No tests taken
{'Maths': 'Failed! (20%)', 'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%
我必须改变什么?
删除行,重置._tests_taken
变量应该更好:)
def take_test(self, testpaper, answers):
self._tests_taken = {} # this row resets the object._tests_taken
pass_mark = 0
你不必重新初始化那个字典,因为在这里:
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
无论如何,您添加(如果再次使用则替换)正确的值。
也将您的 属性 更改为:
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
完整代码:
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self, tests_taken=None):
if tests_taken is None:
tests_taken = {}
self._tests_taken = tests_taken
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
def take_test(self, testpaper, answers):
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
return self._tests_taken
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
return self._tests_taken
if __name__ == '__main__':
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
只需将测试更改为如下所示:
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
并将 __init__
更改为如下所示:
def __init__(self, ):
self._tests_taken = {}
完整代码是这样的:
class Testpaper:
def __init__(self, subject, markscheme, pass_mark):
self.subject = subject
self.markscheme = markscheme
self.pass_mark = pass_mark
class Student:
def __init__(self):
self._tests_taken = {}
@property
def tests_taken(self):
if self._tests_taken == {}:
return "No tests taken"
else:
return self._tests_taken
def take_test(self, testpaper, answers):
self._tests_taken == {}
pass_mark = 0
for answer in answers:
if answer in testpaper.markscheme:
percent = 100 / len(testpaper.markscheme)
pass_mark += percent
if pass_mark >= int(testpaper.pass_mark[:-1]):
self._tests_taken[testpaper.subject] = f"Passed! ({round(pass_mark)}%)"
else:
self._tests_taken[testpaper.subject] = f"Failed! ({round(pass_mark)}%)"
paper1 = Testpaper("Maths", ["1A", "2C", "3D", "4A", "5A"], "60%")
paper2 = Testpaper("Chemistry", ["1C", "2C", "3D", "4A"], "75%")
paper3 = Testpaper("Computing", ["1D", "2C", "3C", "4B", "5D", "6C", "7A"], "75%")
student1 = Student()
student2 = Student()
paper3 = Testpaper('Computing', ['1D', '2C', '3C', '4B', '5D', '6C', '7A'], '75%')
student2 = Student()
student3 = Student()
student2.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
student3.take_test(paper1, ['1C', '2D', '3A', '4C', '5A'])
student3.take_test(paper3, ['1A', '2C', '3A', '4C', '5D', '6C', '7B'])
print(student3.tests_taken)
print(paper3.subject)
print(paper3.markscheme)
print(paper3.pass_mark)
输出:
No tests taken
{'Maths': 'Failed! (20%)', 'Computing': 'Failed! (43%)'}
Computing
['1D', '2C', '3C', '4B', '5D', '6C', '7A']
75%