都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格。

以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下tab和空格了”。

  举例:

  

def MyFirstFunction():
print('This is my first function')

会报错:

D:\Python33>python p7.py
File "p7.py", line 3
print('This is my first function')
^
IndentationError: expected an indented block

而应该在print语句前多加一个tab:

def MyFirstFunction():

       print('This is my first function')

在windows上你可以用editplus看的,虽然我不知道怎么显示出tab,但是他能显示你的文件是没对齐的,我是在ubuntu上编写python的。我用的是vim,你可以设置下vim中tab用空格替换,我在前一篇文章里写过关于这个设置的问题,我现在在补充一下,之前是在系统目录下设置的,现在我要在home目录下设置; 
    首先 cd ~(到你的home目录) 
    然后 ls -a(显示隐藏的文件) 
    找到.vimrc(记得是带个点前缀的) 
    然后编辑这个文件 
    找到如下的命令: 
16 set autoindent 
17 set smartindent 
19 set tabstop=4 
20 set shiftwidth=4 
21 set softtabstop=4 
22 set noexpandtab 
    然后在set tabstop=4前面加上set expandtab同时你需要把set noexpandtab这个注释掉,怎么注释呢,就是加个双引号。我没试过直接注释不加set expandtab,我想也可以(难道默认是expandtab)。这样你可以放心使用VIM了。 
     当然我并不是想就这样完事,我其实想看下我的代码到底哪里会有问题呢,就需要看下他的格式了,主要是看tab,这个可以同样在刚才那个文件里设置,在刚才设置的下面比如说 
在set softtabstop=4下面加上一句set list。这句就可以让你的vim打开的文件显示出tab了,当然也许你觉得显示的不好看,他显示的样子是“^I”一个数字键6上那个符号和一个大写的I表示一个tab,这个不会显示出空格。他还会显示出段落的起始符^和终止符$,如果不知道这2个符号可以看下正则表达式。 
     以上2点设置以后,我想以后你就不会遇到IndentationError: unexpected indent这个很烦人的错误了。 
    以上的set命令都可以单独使用,但是只对当前打开的vim有效,你可以按下shift加冒号,然后输入set XXX,回车然后就有效果了,但是你要再次进入编辑模式才能看到。

  转载自http://dikar.iteye.com/blog/308934

IndentationError: unexpected indent python的更多相关文章

  1. python的IndentationError: unexpected indent python

    都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格.    ...

  2. python: indentationerror: unexpected indent

    以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...

  3. 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent

    初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...

  4. python中遇到的问题:IndentationError: unexpected indent

    在Python中写下列代码的时候,出现错误:IndentationError: unexpected indent 分析:IndentationError是缩进的错误,查看源代码发现names开始的这 ...

  5. Python脚本运行出现语法错误:IndentationError:unexpected indent

    对于py来说典型错误就是缩进,,烦不胜烦,整理一下解决方法:一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: Indentation ...

  6. [每日一记] Python报错 IndentationError: unexpected indent

    IndentationError: unexpected indent 代码缩进出现错误 今天这个报错的原因是,早些时候的代码里Tab和空格混起来用了,,,[不是我...是编辑器的锅 不过我已经把Su ...

  7. IndentationError: unexpected indent

    都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格.    ...

  8. python遇到IndentationError: unexpected indent

    由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可

  9. python ndentationError: unexpected indent

    python 缩进搞了好久,每次都自己看了没什么问题 IndentationError: unexpected indent 每次都是这个错误. 后来查资料是vimrc配置有点问题 我在写代码的时候用 ...

随机推荐

  1. Windows下PHP(Thread Safe与Non Thread Safe)版本说明

    转载“http://www.taoz11.com/archives/300.html” linux下直接下载源码,在服务器上编译即可,发现windows下有4个版本: VC9 x86 Non Thre ...

  2. js中两个感叹号的原理与用法分析(转载记录没找到原帖)

    var foo; alert(!foo);//undifined情况下,一个感叹号返回的是true; alert(!goo);//null情况下,一个感叹号返回的也是true; var o={flag ...

  3. Android上的远程调试

    来源: http://www.seejs.com/archives/296 目录 远程调试概述 使用 Chrome 的 ADB 扩展进行远程调试 1. 安装 ADB 扩展 2. 启用你的移动设备上的 ...

  4. android 复制、粘贴文字

    Android的剪切板(ClipboardManager) 注意:导包的时候 API 11之前: android.text.ClipboardManagerAPI 11之后: android.cont ...

  5. Logistic Regression 模型简介

    逻辑回归(Logistic Regression)是机器学习中的一种分类模型,由于算法的简单和高效,在实际中应用非常广泛.本文作为美团机器学习InAction系列中的一篇, 主要关注逻辑回归算法的数学 ...

  6. akka 文章 博客

    http://blog.csdn.net/wsscy2004/article/category/2430395 Actor生命周期理解 Actor生命周期理解 镇图:Actor内功心法图 Actor的 ...

  7. js里写网页结构, 传函数参数

    如题 "<td align='center' height='30px' width='80px'><a href='javascript:sort(\"&quo ...

  8. Nginx运行Laravel的配置

    修改nginx.conf.修改前记得备份一下,万一改错了还能还原回去. server { listen 80; server_name localhost; set $root_path '/usr/ ...

  9. ComboBox绑定Dictionary做为数据源

    http://www.cnblogs.com/refresh/archive/2012/07/14/2591503.html https://msdn.microsoft.com/zh-cn/libr ...

  10. 【同一直线最多点】 poj 1118+2606+2780

    poj 1118 #include<iostream> using namespace std; #define N 700 struct point {int x,y;} pnt[N]; ...