Zum Hauptinhalt springen

Mini-Projekt: Textprüfer

Anforderungen

  • mind. 3 Prüfungen mit fullmatch
  • freundliche Meldungen
  • Funktionen mit Type Hints
  • optional: sub zum Bereinigen

Beispiellösung

Lösung anzeigen
import re

def ist_plz(text: str) -> bool:
return re.fullmatch(r"\d{5}", text) is not None

def ist_username(text: str) -> bool:
return re.fullmatch(r"[A-Za-z][A-Za-z0-9_]{2,11}", text) is not None

def ist_level_code(text: str) -> bool:
return re.fullmatch(r"L-\d{2}", text) is not None

tests = {
"plz": ["12345", "1234", "12a45"],
"user": ["Luna", "a", "Luna_1"],
"level": ["L-07", "L7", "X-07"],
}

for name, werte in tests.items():
print("===", name, "===")
for w in werte:
if name == "plz":
ok = ist_plz(w)
elif name == "user":
ok = ist_username(w)
else:
ok = ist_level_code(w)
print(w, "->", "OK" if ok else "NEIN")

Kapitel 46 geschafft!

Als Nächstes: Diagramme mit matplotlib.