Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| modul:m323:learningunits:lu02:aufgaben:dataclass2 [2024/08/28 11:04] – [Aufgabenstellung] kmaurizi | modul:m323:learningunits:lu02:aufgaben:dataclass2 [2024/08/28 11:46] (aktuell) – kmaurizi | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== LU02.A11 - Immutable | + | ====== LU02.A11 - Immutable |
| <WRAP center round todo 60%> | <WRAP center round todo 60%> | ||
| Zeile 14: | Zeile 14: | ||
| - Implementieren Sie eine Funktion '' | - Implementieren Sie eine Funktion '' | ||
| - Schreiben Sie ein Programm, das mehrere Noten hinzufügt, den Durchschnitt berechnet und den Studenten " | - Schreiben Sie ein Programm, das mehrere Noten hinzufügt, den Durchschnitt berechnet und den Studenten " | ||
| - | |||
| - | ==== Code Vorlage ==== | ||
| - | |||
| - | <code python> | ||
| - | from student import Student | ||
| - | |||
| - | def add_grade(student, | ||
| - | """ | ||
| - | Returns a new Student instance with the added grade. | ||
| - | """ | ||
| - | new_grades = student.grades + [grade] | ||
| - | return Student(name=student.name, | ||
| - | |||
| - | def calculate_average(student): | ||
| - | """ | ||
| - | Returns the average of the student' | ||
| - | """ | ||
| - | if not student.grades: | ||
| - | return 0.0 | ||
| - | return sum(student.grades) / len(student.grades) | ||
| - | |||
| - | def graduate_student(student): | ||
| - | """ | ||
| - | Graduates the student if the average grade is 70 or above. | ||
| - | """ | ||
| - | average = calculate_average(student) | ||
| - | if average >= 70: | ||
| - | return Student(name=student.name, | ||
| - | return student | ||
| - | |||
| - | if __name__ == ' | ||
| - | student = Student(name=' | ||
| - | student = add_grade(student, | ||
| - | student = add_grade(student, | ||
| - | student = add_grade(student, | ||
| - | |||
| - | print(f' | ||
| - | average = calculate_average(student) | ||
| - | print(f' | ||
| - | |||
| - | student = graduate_student(student) | ||
| - | print(f' | ||
| - | </ | ||
| ==== Schritt für Schritt ==== | ==== Schritt für Schritt ==== | ||
| Zeile 66: | Zeile 23: | ||
| - Führen Sie mehrere Funktionsaufrufe durch, um die Noten zu aktualisieren, | - Führen Sie mehrere Funktionsaufrufe durch, um die Noten zu aktualisieren, | ||
| - | ==== Lösung ==== | ||
| - | |||
| - | <code python> | ||
| - | # student.py | ||
| - | from dataclasses import dataclass, field | ||
| - | from typing import List | ||
| - | |||
| - | @dataclass(frozen=True) | ||
| - | class Student: | ||
| - | name: str | ||
| - | grades: List[int] = field(default_factory=list) | ||
| - | graduated: bool = False | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | # main.py | ||
| - | from student import Student | ||
| - | |||
| - | def add_grade(student, | ||
| - | """ | ||
| - | Returns a new Student instance with the added grade. | ||
| - | """ | ||
| - | new_grades = student.grades + [grade] | ||
| - | return Student(name=student.name, | ||
| - | |||
| - | def calculate_average(student): | ||
| - | """ | ||
| - | Returns the average of the student' | ||
| - | """ | ||
| - | if not student.grades: | ||
| - | return 0.0 | ||
| - | return sum(student.grades) / len(student.grades) | ||
| - | |||
| - | def graduate_student(student): | ||
| - | """ | ||
| - | Graduates the student if the average grade is 70 or above. | ||
| - | """ | ||
| - | average = calculate_average(student) | ||
| - | if average >= 70: | ||
| - | return Student(name=student.name, | ||
| - | return student | ||
| - | |||
| - | if __name__ == ' | ||
| - | student = Student(name=' | ||
| - | student = add_grade(student, | ||
| - | student = add_grade(student, | ||
| - | student = add_grade(student, | ||
| - | |||
| - | print(f' | ||
| - | average = calculate_average(student) | ||
| - | print(f' | ||
| - | |||
| - | student = graduate_student(student) | ||
| - | print(f' | ||
| - | </ | ||
| ---- | ---- | ||
| < | < | ||
| [[https:// | [[https:// | ||