IndentationError: unexpected indent python
都知道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的更多相关文章
- python的IndentationError: unexpected indent python
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格. ...
- python: indentationerror: unexpected indent
以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...
- 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent
初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...
- python中遇到的问题:IndentationError: unexpected indent
在Python中写下列代码的时候,出现错误:IndentationError: unexpected indent 分析:IndentationError是缩进的错误,查看源代码发现names开始的这 ...
- Python脚本运行出现语法错误:IndentationError:unexpected indent
对于py来说典型错误就是缩进,,烦不胜烦,整理一下解决方法:一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: Indentation ...
- [每日一记] Python报错 IndentationError: unexpected indent
IndentationError: unexpected indent 代码缩进出现错误 今天这个报错的原因是,早些时候的代码里Tab和空格混起来用了,,,[不是我...是编辑器的锅 不过我已经把Su ...
- IndentationError: unexpected indent
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格. ...
- python遇到IndentationError: unexpected indent
由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可
- python ndentationError: unexpected indent
python 缩进搞了好久,每次都自己看了没什么问题 IndentationError: unexpected indent 每次都是这个错误. 后来查资料是vimrc配置有点问题 我在写代码的时候用 ...
随机推荐
- hadoop namespace
As underscore(_) is not allowed. It may be the problem if your other configuration are ok. Your conf ...
- CSS的属性
一,可继承属性<!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- Linux下wps字体问题
1.安装symbol-fonts_1.2_all.deb,自行搜索 2.卸载xfonts-mathml字体 sudo apt-get remove xfonts-mathml
- json处理三部曲之第一曲:利用json-lib-xxx.jar处理json
json-lib-xxx.jar包必需5个依赖包(实测): ezmorph-xxx.jar commons-lang-xxx.jar commons-beanutils-xxx.jar commons ...
- linux yum安装mongodb
1.yum -y install mongodb-server mongodb 2.service mongod start #启动mongodb 服务 3. ...
- numpy中的matrix矩阵处理
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- H5的新应用-拖到页面上的元素
-------------------------- <script type="text/javascript"> // ...
- JavaScript 错误 - Throw、Try 和 Catch
http://www.w3school.com.cn/js/js_errors.asp try 语句测试代码块的错误.catch 语句处理错误.throw 语句创建自定义错误. 错误一定会发生 当 J ...
- 通过C++修改系统时间代码
#include <windows.h>#include <stdio.h>#include <iostream>using namespace std;int m ...
- Bank Interest
Bank Interest Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...