mdlAufgabengeneratoren/aufgabengenerator_einheiten_umwandeln_2.py

122 lines
4.7 KiB
Python

import xml.etree.ElementTree as ET
import random
# Einheiten mit ihren Umrechnungsfaktoren
unit_conversion = {
"t": {"factor": 1000, "name": "Kilogramm"},
"kg": {"factor": 1000, "name": "Gramm"},
"g": {"factor": 1000, "name": "Milligramm"},
"ha": {"factor": 10000, "name": "Quadratmeter"},
"": {"factor": 10000, "name": "Quadratcentimeter"},
"a": {"factor": 100, "name": "Quadratmeter"},
"m": {"factor": 1000, "name": "Millimeter"},
"l": {"factor": 1000, "name": "Milliliter"},
"ml": {"factor": 0.001, "name": "Liter"},
"Tesla": {"factor": 1000000, "name": "Mikrotesla"},
"Ampere": {"factor": 1000, "name": "Milliampere"},
"Volt": {"factor": 1000, "name": "Millivolt"},
"Coulomb": {"factor": 1000, "name": "Millikoulomb"},
}
def create_conversion_question(name, questiontext, feedback, correct_answer, tolerance, unit_from, unit_to, tags):
question = ET.Element('question', attrib={'type': 'numerical'})
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')
text_elem.text = questiontext
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'
answer_elem = ET.SubElement(question, 'answer', attrib={'fraction': '100'})
text_elem = ET.SubElement(answer_elem, 'text')
text_elem.text = f"{str(correct_answer).replace('.', ',')} {unit_to}"
tolerance_elem = ET.SubElement(answer_elem, 'tolerance')
tolerance_elem.text = str(tolerance).replace('.', ',')
feedback_elem = ET.SubElement(answer_elem, 'feedback', attrib={'format': 'html'})
text_elem = ET.SubElement(feedback_elem, 'text')
text_elem.text = 'Richtig!'
incorrectfeedback_elem = ET.SubElement(question, 'incorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(incorrectfeedback_elem, 'text')
text_elem.text = 'Falsch.'
# Einheitsangabe hinzufügen
units_elem = ET.SubElement(question, 'units')
unit_elem = ET.SubElement(units_elem, 'unit')
multiplier_elem = ET.SubElement(unit_elem, 'multiplier')
multiplier_elem.text = '1'
unit_name_elem = ET.SubElement(unit_elem, 'unit_name')
unit_name_elem.text = unit_to
unitgradingtype_elem = ET.SubElement(question, 'unitgradingtype')
unitgradingtype_elem.text = '1'
unitpenalty_elem = ET.SubElement(question, 'unitpenalty')
unitpenalty_elem.text = '0.1000000'
showunits_elem = ET.SubElement(question, 'showunits')
showunits_elem.text = '2'
unitsleft_elem = ET.SubElement(question, 'unitsleft')
unitsleft_elem.text = '0'
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
questions = []
# Generiere 10 Umwandlungsfragen
for i in range(1, 11):
# Wähle eine Einheit und berechne die Umwandlung
unit_from = random.choice(list(unit_conversion.keys()))
unit_to = unit_conversion[unit_from]["name"].lower()
conversion_factor = unit_conversion[unit_from]["factor"]
# Erstelle eine Zufallszahl zwischen einem kleinen Wert und einem größeren Wert
number = round(random.uniform(0.001, 10.0), 5) # Beispielwert zwischen 0,001 und 10,0
correct_answer = round(number * conversion_factor, 2)
questiontext = f"Runde {str(number).replace('.', ',')} {unit_from} auf die Einheit {unit_to} genau! (Bitte Einheit angeben!)"
feedback = f"Die korrekte Antwort ist {str(correct_answer).replace('.', ',')} {unit_to}."
tolerance = 0.0049
tags = ["Runden", "Einheiten", "Umwandlung", "Physikalische Einheit"]
questions.append(create_conversion_question(f"Runden von {unit_from} nach {unit_to} {i}", questiontext, feedback, correct_answer, tolerance, unit_from, unit_to, tags))
# Erstelle das Quiz
quiz = ET.Element('quiz')
for q in questions:
quiz.append(q)
# Speichere die XML-Datei
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)
save_to_file(quiz, 'einheiten_umwandlung_quiz.xml')
print("Moodle-XML-Datei 'einheiten_umwandlung_quiz.xml' erfolgreich erstellt.")