【问题标题】:Reverse Tab in Python IDLE (3.7)Python IDLE (3.7) 中的反向选项卡
【发布时间】:2019-03-10 01:52:45
【问题描述】:

Python IDLE 中是否有用于 reverse-tab 的键盘快捷键?

我正在尝试编写 if-elif-else 语句,但无法使用反向制表符正确缩进 elif 语句。我试过 shift+tab 和 shift+ctrl+tab。

例如,我可以这样写……

if x < 0:
    print('Less than Zero')

但是当我尝试添加 elif 语句时...

elif x == 0:
    print('Zero')

我收到以下错误消息:

SyntaxError: unindent does not match any outer indentation level        

【问题讨论】:

    标签: python python-3.x python-idle


    【解决方案1】:

    ctrl+[ 去除高亮代码的缩进。 ctrl+] 缩进。

    如果您查看编辑器顶部的“编辑”菜单,您可以看到所有快捷方式。

    【讨论】:

    • ctrl+[ 快捷方式有效。但是,Python 文档link 中的第 2.1.2 节交互模式说续行以辅助提示(或...)开头。我的 IDLE 不这样做,而是从外壳的最左边缘开始。有什么想法吗?
    • @hseek 你是在完整编辑器还是 REPL 中?如果您在编辑器中打开了文件,则说明您未处于交互模式。不过,这听起来像是一个单独的问题。
    • 我使用的是 Python 3.7.2 Shell。
    • @hseek IDLE Shell 使用完整的语句,而不是行。因此,它不会对续行使用辅助提示。
    • 删除单行缩进的正确方法是使用 Backspace,通常标记为“
    【解决方案2】:

    反向

    ctrl+[

    对面

    ctrl+]

    【讨论】:

      【解决方案3】:

      使用退格键消除。如果在输入任何代码之前这样做是最简单的。如果该行已经有代码,请在按退格键之前将编辑光标放在缩进的末尾和代码的开头之间。请注意,在 IDLE 中,输入和编辑,并提交完整的语句以供执行。因此没有次要提示。示例:

      >>> a, x, y = 1, 2, 3  # Bind some names.  This is one statement.
      >>> if a:  # When you hit return, IDLE see that you entered the header
                 # of a compound statement and indents the next line with a Tab.
                 # Note that I have to use 8 spaces here instead of a tab.
              print(x)  # When you hit return, IDLE keeps the same indent.
                        # Use Backspace [<--] to dedent back to the margin.
      else:   # Do not line up 'else' with 'if' by adding 4 spaces.
              # Since this is another header, an indent is added on the next line.
              print(y)  # No header, indent kept the same.
              # Hit Return to enter blank line
      

      最后的空行表示复合语句已经完成并且应该被执行。在此之前,可以编辑语句的任何部分。

      我有点吃惊的是,有 3 个人会建议选择行或至少缩进并使用 control-[ 的笨拙且困难得多的解决方法。我正在考虑如何让简单的方法更明显。

      我知道,'if'、'elif' 和 'else' 没有排列是很麻烦的。我打算解决这个问题。

      【讨论】:

        【解决方案4】:

        你做的是Spyder上的快捷方式,但是不,空闲时你必须做的:

        Ctrl+[

        相反的情况是:

        Ctrl+]

        【讨论】:

        • 删除单行缩进的正确方法是使用 Backspace,通常标记为“
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多