feat: tutorial class chore: changed comment of codewars file chore: Update README

This commit is contained in:
Sandro Zimmermann 2026-02-27 16:42:21 +01:00
parent 733ad836e5
commit 4022887753
8 changed files with 135 additions and 2 deletions

View File

@ -1 +1,6 @@
Repository for CDS-2020 Programming and Promt Engineering II
# Codewars
* Katas in src/codewars
|Title|Source|Link|
|Find the force of gravity between two objects|src/codewars/kata_force_of_gravity.py|https://www.codewars.com/kata/5b609ebc8f47bd595e000627/|

View File

@ -1,5 +1,3 @@
# https://www.codewars.com/kata/5b609ebc8f47bd595e000627/
units = {
"kg": 1,
"g": 1e-3,

View File

@ -0,0 +1,10 @@
class Konto:
pass
k1 = Konto()
k2 = Konto()
print(k1)
print(k2)
print(k1 is k2)

View File

@ -0,0 +1,30 @@
from dataclasses import dataclass
@dataclass
class Student:
name: str
nr: int
sem: int
@dataclass
class Rechteck:
breite: float
hoehe: float
def flaeche(self) -> float:
return self.breite * self.hoehe
# Validierung
@dataclass
class Produkt:
name: str
preis: float
def __post_init__(self):
if not self.name:
raise ValueError("...")
if self.preis < 0:
raise ValueError("...")

View File

@ -0,0 +1,41 @@
class Hund:
anzahl_hunde = 0
def __init__(self, name: str, rasse: str, alter: int, gewicht: float):
self.name = name
self.rasse = rasse
self.alter = alter
self.gewicht = gewicht
Hund.anzahl_hunde += 1
def __repr__(self):
return f"Hund(name={self.name!r}, rasse={self.rasse}, alter={self.alter}, gewicht={self.gewicht}"
def __str__(self):
return f"{self.name} ist ein {self.alter}-jähriger {self.rasse}"
def bellen(self, n=1) -> int:
print(n * "Woof! ")
def geburtstag(self):
self.alter += 1
print(
f"Alles Gute zum Geburtstag, {self.name}! Du bist jetzt {self.alter} Jahre alt."
)
def ist_welpe(self):
if self.alter < 2:
print(f"{self.name} ist ein {self.alter}-jähriger Welpe")
else:
print(f"{self.name} ist ein {self.alter}-jähriger erwachsener Hund")
hund1 = Hund(name="Bello", rasse="Pudel", alter=99, gewicht=357)
hund2 = Hund(name="Dewy", rasse="Labrador", alter=-6, gewicht=1)
print(repr(hund1))
print(hund2)
hund2.bellen(3)
hund1.geburtstag()
hund2.ist_welpe()
hund1.ist_welpe()

View File

@ -0,0 +1,22 @@
class Konto:
def __init__(self, inhaber, saldo):
if not inhaber:
raise ValueError("Inhaber darf nicht leer sein")
if saldo < 0:
raise ValueError("Saldo darf nicht negativ sein")
self.inhaber = inhaber
self.saldo = saldo
def einzahlen(self, betrag):
self.saldo += betrag
def abheben(self, betrag):
self.saldo -= betrag
k = Konto("Alice", 100)
print(k.saldo)
k.einzahlen(25)
print(k.saldo)
k.abheben(45)
print(k.saldo)

View File

@ -0,0 +1,11 @@
class Konto:
def __init__(self, inhaber, saldo):
self.inhaber = inhaber
self.saldo = saldo
def __repr__(self):
return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})"
k = Konto(" Alice ", 100.0)
print(k) # Konto ( inhaber = Alice , saldo =100.0)

View File

@ -0,0 +1,16 @@
class Konto:
def __init__(self, inhaber, saldo):
self.inhaber = inhaber
self.saldo = saldo
def __repr__(self):
return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})"
def __str__(self):
return f"Konto von {self.inhaber}: CHF {self.saldo:.2f}"
k = Konto("Alice", 100.0)
print(str(k)) # __str__
print(repr(k)) # __repr__
print(k) # bevorzugt __str__ , sonst __repr__