2015-10-29 4 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? –

Смежные вопросы