From d97031c44346809f9c647d3193cf74c161a9b7d7 Mon Sep 17 00:00:00 2001 From: Martin Putzlocher Date: Wed, 27 Nov 2024 17:35:59 +0100 Subject: [PATCH] =?UTF-8?q?Aufgaben=20mit=20drei=20Br=C3=BCchen=20und=20K?= =?UTF-8?q?=C3=BCrzen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...rator_brueche_addieren_gleichnamig_v5-1.py | 148 ++++++++++++++++++ ...nerator_brueche_addieren_gleichnamig_v5.py | 145 +++++++++++++++++ 2 files changed, 293 insertions(+) create mode 100644 aufgabengenerator_brueche_addieren_gleichnamig_v5-1.py create mode 100644 aufgabengenerator_brueche_addieren_gleichnamig_v5.py diff --git a/aufgabengenerator_brueche_addieren_gleichnamig_v5-1.py b/aufgabengenerator_brueche_addieren_gleichnamig_v5-1.py new file mode 100644 index 0000000..e0689bd --- /dev/null +++ b/aufgabengenerator_brueche_addieren_gleichnamig_v5-1.py @@ -0,0 +1,148 @@ +import xml.etree.ElementTree as ET +import random +from math import gcd + +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 die Tabelle für die Lücken + cloze_text = "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + + # Prüfe, ob der Bruch gekürzt werden kann + if gcd(correct_answers[0], correct_answers[1]) > 1: + common_divisor = gcd(correct_answers[0], correct_answers[1]) + reduced_numerator = correct_answers[0] // common_divisor + reduced_denominator = correct_answers[1] // common_divisor + + cloze_text += "" + cloze_text += f"" + cloze_text += "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + + cloze_text += "
Zähler: {{1:SHORTANSWER:={correct_answers[0]}~%0%{wrong_answers[0][0]}#{wrong_answers[0][1]}}}
Nenner: {{1:SHORTANSWER:={correct_answers[1]}~%0%{wrong_answers[1][0]}#{wrong_answers[1][1]}}}
Gekürzter Zähler: {{1:SHORTANSWER:={reduced_numerator}~%0%{wrong_answers[2][0]}#{wrong_answers[2][1]}}}
Gekürzter Nenner: {{1:SHORTANSWER:={reduced_denominator}~%0%{wrong_answers[3][0]}#{wrong_answers[3][1]}}}
" + + # Füge die Frage hinzu + text_elem.text = questiontext + " " + cloze_text + + generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(generalfeedback_elem, 'text') + + # Musterlösung im LaTeX-Format + if gcd(correct_answers[0], correct_answers[1]) > 1: + solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)." + else: + solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)." + + text_elem.text = solution + + 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 n Aufgaben zur Addition und Subtraktion von gleichnamigen Brüchen +n = 20 +for i in range(1, (n+1)): + # 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 + numerator3 = random.randint(1, 200) # Zähler zwischen 1 und 200 + + operations = random.choice(["+ +", "+ -", "- +", "- -"]) # Zufällige Operationen + if operations == "+ +": + result_numerator = numerator1 + (numerator2 + numerator3) + elif operations == "+ -": + result_numerator = numerator1 + (numerator2 - numerator3) + elif operations == "- +": + result_numerator = numerator1 - (numerator2 + numerator3) + else: + result_numerator = numerator1 - (numerator2 - numerator3) + + # Prüfe, ob der Bruch gekürzt werden kann + needs_reduction = gcd(result_numerator, denominator) > 1 + + # Frage im LaTeX-Format + questiontext = f"

Berechne und kürze das Ergebnis, wenn möglich:

\\( \\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator}}} {operations[0]} \\left(\\frac{{{numerator2}}}{{{denominator}}} {operations[2]} \\frac{{{numerator3}}}{{{denominator}}}\\right) = \\)

" + + # Antworten + correct_answers = [result_numerator, denominator] + if needs_reduction: + common_divisor = gcd(result_numerator, denominator) + reduced_numerator = result_numerator // common_divisor + reduced_denominator = denominator // common_divisor + + correct_answers.extend([reduced_numerator, reduced_denominator]) # Füge gekürzte Antworten hinzu + + 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 + (reduced_numerator + random.randint(1, 3), "Dieser gekürzte Zähler ist nicht korrekt."), # Falscher gekürzter Zähler + (reduced_denominator - random.randint(1, 3), "Dieser gekürzte Nenner ist nicht korrekt.") # Falscher gekürzter Nenner + ] + else: + 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 3 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.") \ No newline at end of file diff --git a/aufgabengenerator_brueche_addieren_gleichnamig_v5.py b/aufgabengenerator_brueche_addieren_gleichnamig_v5.py new file mode 100644 index 0000000..3e0b3e0 --- /dev/null +++ b/aufgabengenerator_brueche_addieren_gleichnamig_v5.py @@ -0,0 +1,145 @@ +import xml.etree.ElementTree as ET +import random +from math import gcd + +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 die Tabelle für die Lücken + cloze_text = "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + + # Prüfe, ob der Bruch gekürzt werden kann + if gcd(correct_answers[0], correct_answers[1]) > 1: + common_divisor = gcd(correct_answers[0], correct_answers[1]) + reduced_numerator = correct_answers[0] // common_divisor + reduced_denominator = correct_answers[1] // common_divisor + + cloze_text += "" + cloze_text += f"" + cloze_text += "" + cloze_text += "" + cloze_text += f"" + cloze_text += "" + + cloze_text += "
Zähler: {{1:SHORTANSWER:={correct_answers[0]}~%0%{wrong_answers[0][0]}#{wrong_answers[0][1]}}}
Nenner: {{1:SHORTANSWER:={correct_answers[1]}~%0%{wrong_answers[1][0]}#{wrong_answers[1][1]}}}
Gekürzter Zähler: {{1:SHORTANSWER:={reduced_numerator}~%0%{wrong_answers[2][0]}#{wrong_answers[2][1]}}}
Gekürzter Nenner: {{1:SHORTANSWER:={reduced_denominator}~%0%{wrong_answers[3][0]}#{wrong_answers[3][1]}}}
" + + # Füge die Frage hinzu + questiontext = questiontext + + text_elem.text = questiontext + " " + cloze_text + + generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(generalfeedback_elem, 'text') + + # Musterlösung im LaTeX-Format + if gcd(correct_answers[0], correct_answers[1]) > 1: + solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)." + else: + solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)." + + text_elem.text = solution + + 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 n Aufgaben zur Addition und Subtraktion von gleichnamigen Brüchen +n = 20 +for i in range(1, (n+1)): + # 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 + + # Prüfe, ob der Bruch gekürzt werden kann + needs_reduction = gcd(result_numerator, denominator) > 1 + + # Frage im LaTeX-Format + questiontext = f"

Berechne und kürze das Ergebnis, wenn möglich:

\( \\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator}}} {operation} \\frac{{{numerator2}}}{{{denominator}}} = \)

" + + # Antworten + correct_answers = [result_numerator, denominator] + if needs_reduction: + common_divisor = gcd(result_numerator, denominator) + reduced_numerator = result_numerator // common_divisor + reduced_denominator = denominator // common_divisor + + correct_answers.extend([reduced_numerator, reduced_denominator]) # Füge gekürzte Antworten hinzu + + 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 + (reduced_numerator + random.randint(1, 3), "Dieser gekürzte Zähler ist nicht korrekt."), # Falscher gekürzter Zähler + (reduced_denominator - random.randint(1, 3), "Dieser gekürzte Nenner ist nicht korrekt.") # Falscher gekürzter Nenner + ] + else: + 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.") \ No newline at end of file