Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung | |||
| modul:m323:learningunits:lu02:aufgaben:dataclass1 [2024/08/28 11:30] – kmaurizi | modul:m323:learningunits:lu02:aufgaben:dataclass1 [2024/08/28 11:39] (aktuell) – kmaurizi | ||
|---|---|---|---|
| Zeile 15: | Zeile 15: | ||
| - Nutzen Sie die Funktionen, um mehrere Änderungen an einer Instanz von '' | - Nutzen Sie die Funktionen, um mehrere Änderungen an einer Instanz von '' | ||
| - | ==== Code Vorlage ==== | ||
| - | |||
| - | <code python> | ||
| - | from car import Car | ||
| - | |||
| - | def add_mileage(car, | ||
| - | """ | ||
| - | Returns a new Car instance with updated mileage and possibly an updated service date. | ||
| - | """ | ||
| - | new_mileage = car.mileage + distance | ||
| - | new_service_dates = car.service_dates[: | ||
| - | | ||
| - | if new_mileage > 10000 and (car.mileage <= 10000): | ||
| - | new_service_dates.append(' | ||
| - | | ||
| - | return Car(brand=car.brand, | ||
| - | |||
| - | def check_service(car): | ||
| - | """ | ||
| - | Checks if the car's mileage exceeds 10,000 km. | ||
| - | """ | ||
| - | return car.mileage > 10000 | ||
| - | |||
| - | if __name__ == ' | ||
| - | my_car = Car(brand=' | ||
| - | print(f' | ||
| - | | ||
| - | my_car = add_mileage(my_car, | ||
| - | print(f' | ||
| - | | ||
| - | if check_service(my_car): | ||
| - | print(' | ||
| - | |||
| - | my_car = add_mileage(my_car, | ||
| - | print(f' | ||
| - | print(f' | ||
| - | </ | ||
| ==== Schritt für Schritt ==== | ==== Schritt für Schritt ==== | ||
| Zeile 59: | Zeile 22: | ||
| - Implementieren Sie die Funktion '' | - Implementieren Sie die Funktion '' | ||
| - Führen Sie mehrere Funktionsaufrufe durch, um die Änderungen zu testen und die Liste der Service-Daten zu überprüfen. | - Führen Sie mehrere Funktionsaufrufe durch, um die Änderungen zu testen und die Liste der Service-Daten zu überprüfen. | ||
| - | |||
| - | ==== Lösung ==== | ||
| - | |||
| - | <code python> | ||
| - | # car.py | ||
| - | from dataclasses import dataclass, field | ||
| - | from typing import List | ||
| - | |||
| - | @dataclass(frozen=True) | ||
| - | class Car: | ||
| - | brand: str | ||
| - | mileage: int | ||
| - | service_dates: | ||
| - | </ | ||
| - | |||
| - | <code python> | ||
| - | # main.py | ||
| - | from car import Car | ||
| - | |||
| - | def add_mileage(car, | ||
| - | """ | ||
| - | Returns a new Car instance with updated mileage and possibly an updated service date. | ||
| - | """ | ||
| - | new_mileage = car.mileage + distance | ||
| - | new_service_dates = car.service_dates[: | ||
| - | | ||
| - | if new_mileage > 10000 and (car.mileage <= 10000): | ||
| - | new_service_dates.append(' | ||
| - | | ||
| - | return Car(brand=car.brand, | ||
| - | |||
| - | def check_service(car): | ||
| - | """ | ||
| - | Checks if the car's mileage exceeds 10,000 km. | ||
| - | """ | ||
| - | return car.mileage > 10000 | ||
| - | |||
| - | if __name__ == ' | ||
| - | my_car = Car(brand=' | ||
| - | print(f' | ||
| - | | ||
| - | my_car = add_mileage(my_car, | ||
| - | print(f' | ||
| - | | ||
| - | if check_service(my_car): | ||
| - | print(' | ||
| - | |||
| - | my_car = add_mileage(my_car, | ||
| - | print(f' | ||
| - | print(f' | ||
| - | </ | ||
| ---- | ---- | ||
| < | < | ||
| [[https:// | [[https:// | ||