import xml.etree.ElementTree as ET import random def create_cloze_question(name, questiontext, correct_answers, wrong_answers, feedback, tags): question = ET.Element('question', attrib={'type': 'cloze'}) name_elem = ET.SubElement(question, 'name') text_elem = ET.SubElement(name_elem, 'text') text_elem.text = name questiontext_elem = ET.SubElement(question, 'questiontext', attrib={'format': 'html'}) text_elem = ET.SubElement(questiontext_elem, 'text') # Erstelle den Cloze-Text cloze_text = questiontext for i in range(len(correct_answers)): correct_answer = correct_answers[i] wrong_answer, wrong_feedback = wrong_answers[i] cloze_text = cloze_text.replace("______", f"{{1:SHORTANSWER:={correct_answer}#{feedback}~%0%{wrong_answer}#{wrong_feedback}}}", 1) text_elem.text = cloze_text generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'}) text_elem = ET.SubElement(generalfeedback_elem, 'text') text_elem.text = feedback defaultgrade_elem = ET.SubElement(question, 'defaultgrade') defaultgrade_elem.text = '1.0000000' penalty_elem = ET.SubElement(question, 'penalty') penalty_elem.text = '0.3333333' hidden_elem = ET.SubElement(question, 'hidden') hidden_elem.text = '0' # Tags hinzufügen tags_elem = ET.SubElement(question, 'tags') for tag in tags: tag_elem = ET.SubElement(tags_elem, 'tag') text_elem = ET.SubElement(tag_elem, 'text') text_elem.text = tag return question def create_quiz(questions): quiz = ET.Element('quiz') for q in questions: quiz.append(q) return quiz def save_to_file(xml_element, filename): tree = ET.ElementTree(xml_element) with open(filename, 'wb') as f: tree.write(f, encoding='utf-8', xml_declaration=True) questions = [] # Generiere 12 Aufgaben zur Addition und Subtraktion von gleichnamigen Brüchen for i in range(1, 13): # Zähler und Nenner generieren denominator = random.randint(2, 20) # Nenner zwischen 2 und 20 numerator1 = random.randint(1, 200) # Zähler zwischen 1 und 200 numerator2 = random.randint(1, 200) # Zähler zwischen 1 und 200 operation = random.choice(["+", "-"]) # Zufällige Operation if operation == "+": result_numerator = numerator1 + numerator2 else: result_numerator = numerator1 - numerator2 # Frage im LaTeX-Format questiontext = f"Berechne \( \\frac{{{numerator1}}}{{{denominator}}} {operation} \\frac{{{numerator2}}}{{{denominator}}} = \) (Zähler: ______, Nenner: {denominator})" # Antworten correct_answers = [result_numerator, denominator] wrong_answers = [ (result_numerator + random.randint(1, 5), "Dieser Zähler ist zu hoch."), # Falscher Zähler (result_numerator - random.randint(1, 5), "Dieser Zähler ist zu niedrig.") # Falscher Zähler ] feedback = "Richtig! Die Lösung ist korrekt." # Frage erstellen und hinzufügen questions.append(create_cloze_question(f"Addition/Subtraktion von Brüchen {i}", questiontext, correct_answers, wrong_answers, feedback, ["Mathematik", "Brüche", "Addition", "Subtraktion"])) # Erstelle das Quiz quiz = create_quiz(questions) # Speichere die XML-Datei save_to_file(quiz, 'cloze_fraction_addition_subtraction_quiz.xml') print("Moodle-XML-Datei 'cloze_fraction_addition_subtraction_quiz.xml' erfolgreich erstellt.")