вроде как использовал кодировку 'utf-8', всё равно ничего не работает

Рейтинг: -2Ответов: 1Опубликовано: 28.06.2023
import sys

def open_file(file_name, mode):
    """Открывает файл"""
    try:
        the_file = open(file_name, mode, encoding='utf-8')
        print('Исполняется')
    except IOError as e:
        print("Невозможно открыть файл", file_name, "Роабота программы будет завершена.\n", e)
        input("Нажмите Enter, чтобы выйти")
        sys.exit()
    else:
        print('Исполняется')
        return the_file


def next_line(the_file):
    """Возвращает в отформатированном формате очередную строку игрового файла"""
    line = the_file.readline()
    line = line.replace('/', "\n")
    return line


def next_block(the_file):
    """Вощвращает очредной блок данных из игрового файла"""
    category = next_line(the_file)
    question = next_line(the_file)
    answers = []
    for i in range(4):
        answers.append(next_line(the_file))
        correct = next_line(the_file)
        if correct:
            correct = correct[0]
            explanation = next_line(the_file)
    return category, question, answers, correct, explanation


def welcome(title):
    """Приветствует игорка и сообщает ему тему"""
    print('\t\tДобро пожаловать в игру викторина')
    print('\t\t', title, '\n')


def main():
    the_file = open("Текст викторины.txt", "r")
    title = next_line(the_file=the_file)
    welcome(title=title)
    score = 0
    category, question, answers, correct, explanation = next_block(the_file)


    while category:
        print(category)
        print(question)
        for i in range(4):
            print("\t", i + 1, answers[i])
        answer = input("Ваш ответ>>>")
        if answer == correct:
            print('\nДа!', end=' ')
            score += 1
        else:
            print('Нет', end=' ')
        print(explanation)
        print(score, '\n\n')
        category, question, answers, correct, explanation = next_block(the_file)


    the_file.close()
    print('Это был последнирй вопрос')
    print('На вашем счете', score)


main()
input("Нажмите Enter, чтобы выйти")

Ответы

▲ 0Принят
def open_file(file_name, mode):
    """Открывает файл"""
    try:
        the_file = open(file_name, mode, encoding='utf-8')

Тут mode - чего-то не хватает.

def main():
    the_file = open("Текст викторины.txt", "r")

Тут нет encoding='utf8' по этому другая кодировка.

Дальше ничего не видно т.к. нет какого-то текста.