Created
November 30, 2023 10:13
-
-
Save ErhardRainer/3ed7f4f93f909cdd9076388bdf0e03ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_color(text, color, background=None): | |
""" | |
Druckt den angegebenen Text in der angegebenen Farbe und optional auf einem farbigen Hintergrund. | |
Args: | |
- text (str): Der zu druckende Text. | |
- color (str): Die Farbe des Textes. | |
- background (str, optional): Die Hintergrundfarbe. | |
""" | |
colors = { | |
"schwarz": "30", | |
"rot": "31", | |
"grün": "32", | |
"gelb": "33", | |
"blau": "34", | |
"magenta": "35", | |
"cyan": "36", | |
"weiß": "37" | |
} | |
backgrounds = { | |
"schwarz": "40", | |
"rot": "41", | |
"grün": "42", | |
"gelb": "43", | |
"blau": "44", | |
"magenta": "45", | |
"cyan": "46", | |
"weiß": "47" | |
} | |
color_code = colors.get(color.lower()) | |
background_code = backgrounds.get(background.lower()) if background else "" | |
if color_code: | |
if background_code: | |
print(f"\033[{color_code};{background_code}m{text}\033[0m") | |
else: | |
print(f"\033[{color_code}m{text}\033[0m") | |
else: | |
print("Unbekannte Farbe.") | |
# Beispielaufrufe | |
print_color("Dies ist ein roter Text.", "rot") | |
print_color("Dies ist ein grüner Text auf gelbem Hintergrund.", "grün", "gelb") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment