PEP 8 in der Dokumentation
Bisher haben wir uns darauf konzentriert, wie PEP 8 Funktionscode beeinflusst. Es gibt aber auch Regeln, die Kommentare und Dokumentation besser lesbar machen. In dieser Übung korrigierst du verschiedene Arten von Kommentaren, damit sie PEP-8-konform sind.
Das Ergebnis einer pycodestyle-Stilprüfung des Codes siehst du unten.
my_script.py:2:15: E261 at least two spaces before inline comment
my_script.py:5:16: E262 inline comment should start with '# '
my_script.py:11:1: E265 block comment should start with '# '
my_script.py:13:2: E114 indentation is not a multiple of four (comment)
my_script.py:13:2: E116 unexpected indentation (comment)
Diese Übung ist Teil des Kurses
Grundlagen der Softwareentwicklung in Python
Anleitung zur Übung
- Nutze die Ausgabe von
pycodestyle, um die Kommentare im Code so zu bearbeiten, dass sie PEP-8-konform sind.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
def print_phrase(phrase, polite=True, shout=False):
if polite:# It's generally polite to say please
phrase = 'Please ' + phrase
if shout: #All caps looks like a written shout
phrase = phrase.upper() + '!!'
print(phrase)
#Politely ask for help
print_phrase('help me', polite=True)
# Shout about a discovery
print_phrase('eureka', shout=True)