/
MakarovaSS
/
TestProject
Обзор
Документация
Войти
/
MakarovaSS
/
TestProject
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
new_main
script.py
106 строк
4 KB
MakarovaSS
Создан скрипт-генератор тестов script.py. Входные данные: input.md
26 май 2026, 18:40
26 май 2026, 18:40
674b717
Код
Авторство
О чём код?
# script.py def parse_input_file(file_path): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() tasks = content.strip().split('#Task') quiz_data = [] for task in tasks: if not task.strip(): continue lines = task.strip().split('\n') question = "" variants = [] correct_variant = "" for line in lines: if line.startswith('##Question'): question = line[len('##Question'):].strip() elif line.startswith('##Correct Variant'): correct_variant = line[len('##Correct Variant'):].strip() variants.append(correct_variant) elif line.startswith('##Variant'): variant_text = line[len('##Variant'):].strip() variants.append(variant_text) quiz_data.append({ 'question': question, 'variants': variants, 'correct': correct_variant }) return quiz_data def generate_html_form(quiz_data, output_file='quiz.html'): html = """<html> <head> <title>Тест по наследованию</title> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; margin: 40px; } .question { margin-bottom: 20px; } .variants { margin-left: 20px; } input[type=radio] { margin-right: 5px; } button { padding: 10px 20px; font-size: 16px; margin: 5px 0; } .result { margin-top: 20px; font-weight: bold; font-size: 18px; } </style> </head> <body> <h1>Тестирование по ООП: Наследование</h1> <form id="quizForm"> """ for idx, q in enumerate(quiz_data): html += f' <div class="question">\n <p><strong>{idx + 1}. {q["question"]}</strong></p>\n <div class="variants">\n' for v_idx, variant in enumerate(q['variants']): option_id = f"q{idx}_v{v_idx}" html += f' <input type="radio" name="q{idx}" value="{variant}" id="{option_id}"> <label for="{option_id}">{variant}</label><br>\n' html += ' </div>\n </div>\n' html += """ <button type="button" onclick="checkAnswers()">Проверить ответы</button> <button type="button" onclick="resetQuiz()">Пройти тест ещё раз</button> </form> <div class="result" id="result"></div> <script> function checkAnswers() { const resultDiv = document.getElementById("result"); let correctCount = 0; let totalCount = 0; """ for idx, q in enumerate(quiz_data): correct = q['correct'].replace('"', '\\"') html += f' const selectedQ{idx} = document.querySelector(\'input[name="q{idx}"]:checked\');\n' html += f' if (selectedQ{idx} && selectedQ{idx}.value === "{correct}") {{\n' html += f' correctCount++;\n' html += f' }}\n' html += f' totalCount++;\n' html += """ resultDiv.innerHTML = `Правильно ${correctCount} из ${totalCount}.`; } function resetQuiz() { // Снимаем все выбранные радио-кнопки document.querySelectorAll('input[type=radio"]').forEach(radio => { radio.checked = false; }); // Очищаем результат document.getElementById("result").innerHTML = ""; } </script> </body> </html>""" with open(output_file, 'w', encoding='utf-8') as f: f.write(html) print(f"Форма успешно создана: {output_file}") if __name__ == "__main__": data = parse_input_file("input.md") generate_html_form(data)