Python 2.7缩进错误 [英] Python 2.7 IndentationError

查看:92
本文介绍了Python 2.7缩进错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Python解释器中运行程序时出现IndentationError:

I am getting an IndentationError when trying to run my program in a Python Interpreter:

 line 127
    global map
         ^
IndentationError: expected an indented block

我正在使用python版本2.7

I am using python version 2.7

以下代码有什么问题?:

What's wrong with the following code?:

def make_map():
global map

推荐答案

Python期望4个空格或一个制表符来缩进和对齐代码-与Java相似,期望大括号{}是循环,方法或类的开始.

Python expects 4 spaces or a tab to indent and align code - similar to Java expecting curly {} brackets are the start of a loop, method or class etc.

def some_function():
somecode
morecode
...

应设置为

def some_function():
    somecode
    morecode
    ...

您的代码似乎在第127行上引发了异常,因此请检查此代码并根据需要缩进代码.

It appears that your code throws an exception on line 127, so check this and indent the code as required.

def some_code():
    for i in range(1, some_value):
        some_method()

        if need_more_indent:
            indent_code()

        do_this_after_indent_code()

    this_runs_after_for_loop()

    return 'lol'

这篇关于Python 2.7缩进错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆