2015-10-29 100 views
0
class DetailedScore(Score): 
'''A subclass of Score adding level''' 

    def __init__(self, points, initials, level): 
     ''' 
     (Score, int, str, int) -> NoneType 

     Create a score including number of points, initials, and level. 
     ''' 

     super().__init__(points, initials) 
     self.level = level 

    def __str__(self): 
     ''' 
     Return a string representation of DetailedScore formated: 

     'The student with initials 'KTH' scored 100 points, the student is in level 10' 
     ''' 

     score_str = super().__str__() 

     return '{}, the student is in level {}'.format(score_str, self.level) 

    def __repr__(self): 
     ''' 
     Return a string representation of DetailedScore formated: 

     'DetailedScore(100, 'KTH', 10)' 
     ''' 

     return 'DetailedScore({}, {}, {})'.format(self.points, self.initials, self.level) 

score5 = DetailedScore(1000, 'JQP', 100) 
score6 = DetailedScore(999, 'ABC', 99) 
score7 = DetailedScore(999, 'BBB', 15) 
score8 = DetailedScore(1, 'KTH', 12) 

我想完成这个类,并且不知道为什么在尝试构建时我总是收到错误。执行类时发生Python错误

这是错误:

Traceback (most recent call last): 
    File "/Users/KoryHershock/Documents/Python/[Kory_Hershock]_final.py", line 187, in <module> 
    score5 = DetailedScore(1000, 'JQP', 100) 
    File "/Users/KoryHershock/Documents/Python/[Kory_Hershock]_final.py", line 162, in __init__ 
    super().__init__(points, initials) 
TypeError: super() takes at least 1 argument (0 given) 
[Finished in 0.1s with exit code 1] 

回答

4

如果你正在使用Python 2,你必须写super(DetailedScore, self),不super()

Python 3还允许编译器插入从词法上下文中获取的适当类对象的无参数形式。

+0

我在我的电脑上安装了python 3,我将如何让Sublime Text 2运行它?或者我将如何通过终端运行python 3? –

+0

@KoryHershock Python 3可执行文件通常称为“python3”。如何在Sublime中进行设置可能是在Sublime相关论坛中最好的问题。 – user4815162342

2

变化super().__whatever__()super(Score, self).__whatever__()

+0

感谢您的回复,试图运行安装在我的电脑上的python3,我将如何让Sublime Text 2运行它?或者我将如何在python3中运行终端程序? –

相关问题