0

Phyton Классы При попытке запуска кода выдаёт ошибку missing 2 required positional arguments: 'self' and 'file'

Сама ошибка:

Traceback (most recent call last): File "C:\Users\alexo_q4humrs\PycharmProjects\classes\main.py", line 26, in books.fill_books() TypeError: fill_books() missing 2 required positional arguments: 'self' and 'file'


from datetime import datetime
f = open('books.txt', 'r',  encoding='utf-8')

class Books:
    def __init__(self):
        self.b_name = ['']*5
        self.b_author = ['']*5
        self.b_date = ['']*5


    def fill_books(self, file):
        f = file.readlines()
        for i in range(1, 5):
            self.b_author = f[i * 3]
            self.b_name[i] = f[1 + (i - 1) * 3]
            self.b_date[i] = (f'{f[2 + (i - 1) * 3]},1,1')
        self.b_author[0] = f[0]
        self.b_name[0] = f[1]
        self.b_date[0] = f[2]

    def get_books(self):
        for i in range(5):
            print(f'{self.b_author[0]} {self.b_name[0]} {self.b_date[0]}')
if __name__ == '__main__':
    books = Books
    books.fill_books()
    books.get_books()
'''




4
  • 3
    books = Books — хорошенько подумайте, что вы делаете в этой строке
    – andreymal
    20 янв в 15:30
  • 1
    А потом то же самое про строчку books.fill_books() 20 янв в 15:40
  • @andreymal можно чуть-чуть конкретнее в чём здесь дело? 20 янв в 15:40
  • @andreymal забыл добавить скобки, спасибо за намёк 20 янв в 15:44

1 ответ 1

0

Готовый код

from datetime import datetime
f = open('books.txt', 'r',  encoding='utf-8')

class Books:
    def __init__(self):
        self.b_name = ['']*5
        self.b_author = ['']*5
        self.b_date = ['']*5


    def fill_books(self, file):
        f = file.readlines()
        for i in range(1, 5):
            self.b_author[i] = f[i * 3]
            self.b_name[i] = f[1 + (i - 1) * 3]
            self.b_date[i] = (f'{f[2 + (i - 1) * 3]},1,1')
        self.b_author[0] = f[0]
        self.b_name[0] = f[1]
        self.b_date[0] = f[2]

    def get_books(self):
        for i in range(5):
            print(f'{self.b_author[0]} {self.b_name[0]} {self.b_date[0]}')
if __name__ == '__main__':
    books = Books()
    books.fill_books(f)
    books.get_books()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.