所以有两个问题。我在Sublime Text 3中有这段代码,在这里我使用Anaconda包(相关空白将显示为,如ST3所示;Python.Sublime-settings显示在本文末尾):

elif choice == "taunt bear" and not bear_moved:
••••••••print("The•bear•has•moved•from•the•door.•You•can•go•through•it•now"
            )
        bear_moved = True

第一个问题:
我启用了自动换行,标尺设置为80,所以它会自动换行代码,在带有“print…”的行上正好有79个字符,在下一行有1个字符。但是linter给了我一个错误“[W]PEP8(E501):行太长(80>79个字符)”。我在下一行有右括号自动缩进,所以没有违反“每行79个字符”的规则。有什么问题吗?是不是这条线应该总是小于80个字符,即使它跨越多条线?
第二个问题:
elif choice == "taunt bear" and not bear_moved:
••••••••print("""The•bear•has•moved•from•the•door.
•••••••••••••••••You•can•go•through•it•now""")
        bear_moved = True

在这里,我想去掉“>79个字符”错误,并创建一个多行字符串。问题是,当我把字符串中的两个句子分成两行,以便能够根据PEP8规则对齐它们时,我必须缩进它们,缩进意味着字符串中有过多的空白,这不是我想要的。下面是理想情况下的工作方式,在第一个句子的结尾处有一个需要的空格字符,并且没有空格用于缩进字符串的后半部分:
elif choice == "taunt bear" and not bear_moved:
••••••••print("""The•bear•has•moved•from•the•door.•
                 You•can•go•through•it•now""")
        bear_moved = True

Python.sublime-设置:
{
    // editor options
    "draw_white_space": "all",

    // tabs and whitespace
    "auto_indent": true,
    "rulers": [80],
    "smart_indent": true,
    "tab_size": 4,
    "trim_automatic_white_space": true,
    "use_tab_stops": false,
    "word_wrap": true,
    "wrap_width": 80
}

最佳答案

您可以使用Python string concat机制并用单引号编写字符串,如示例所示:

elif choice == "taunt bear" and not bear_moved:
••••••••print("The•bear•has•moved•from•the•door.•"
              "You•can•go•through•it•now")
        bear_moved = True

所以你的代码是符合PEP8的,字符串是所需的格式。

关于python - 短绒“行过长”错误和ST3中多行字符串内的空白过多,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37210471/

10-12 16:53
查看更多