开始使用免费开始使用

文档中的 PEP 8

到目前为止,我们主要关注 PEP 8 如何影响可执行的函数式代码。实际上,也有一些规则用于提升注释和文档的可读性。在本练习中,您将修正多种类型的注释,使其符合 PEP 8。

下面是对该代码运行 pycodestyle 风格检查的结果。

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)

本练习是课程的一部分

Python 中的软件工程原理

查看课程

练习说明

  • 利用 pycodestyle 的输出,编辑代码中的注释,使其符合 PEP 8。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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)
编辑并运行代码