feat: tutorial class chore: changed comment of codewars file chore: Update README
This commit is contained in:
parent
733ad836e5
commit
4022887753
@ -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/|
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
# https://www.codewars.com/kata/5b609ebc8f47bd595e000627/
|
||||
|
||||
units = {
|
||||
"kg": 1,
|
||||
"g": 1e-3,
|
||||
|
||||
10
src/tutorial/class/class_introduction.py
Normal file
10
src/tutorial/class/class_introduction.py
Normal file
@ -0,0 +1,10 @@
|
||||
class Konto:
|
||||
pass
|
||||
|
||||
|
||||
k1 = Konto()
|
||||
k2 = Konto()
|
||||
|
||||
print(k1)
|
||||
print(k2)
|
||||
print(k1 is k2)
|
||||
30
src/tutorial/class/dataclass_tutorial.py
Normal file
30
src/tutorial/class/dataclass_tutorial.py
Normal 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("...")
|
||||
41
src/tutorial/class/hund.py
Normal file
41
src/tutorial/class/hund.py
Normal 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()
|
||||
22
src/tutorial/class/konto.py
Normal file
22
src/tutorial/class/konto.py
Normal 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)
|
||||
11
src/tutorial/class/repr_tutorial.py
Normal file
11
src/tutorial/class/repr_tutorial.py
Normal 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)
|
||||
16
src/tutorial/class/str_tutorial.py
Normal file
16
src/tutorial/class/str_tutorial.py
Normal 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__
|
||||
Loading…
x
Reference in New Issue
Block a user