PYTHON 错误:IndentationError:unindent 不匹配任何外部缩进级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18556357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PYTHON ERROR: IndentationError: unindent does not match any outer indentation level
提问by Shahid Rogers
When running this piece of code:
运行这段代码时:
#timestamp, capture, and tweet an image
def stamp_cap_tweet():
timestamp = time.strftime("%d%m%Y-%H%M%S")
os.system("fswebcam -r 960x720 -d /dev/video0 " + timestamp + ".jpg") #save image to disk
twit.update_status_with_media(timestamp + ".jpg", status = "@shahidrogers " + timestamp) #tweet image @username
print "Tweeted image at " + timestamp #make a record in the Python output
I get the error
我收到错误
File "tweetpicture.py", line 17
os.system("fswebcam -r 960x720 -d /dev/video0 " + timestamp + ".jpg") ^ IndentationError:
unindent does not match any outer indentation level
文件“tweetpicture.py”,第 17 行
os.system("fswebcam -r 960x720 -d /dev/video0 " + timestamp + ".jpg") ^ IndentationError:
unindent 不匹配任何外部缩进级别
What could this possibly mean? I have searched around and people have said that there's a mix of tab and spaces, but I do not understand this at all as I am still very new to Python and this is my first few lines of coding.
这可能意味着什么?我四处搜索,人们说有制表符和空格的混合,但我根本不明白这一点,因为我对 Python 仍然很陌生,这是我的前几行编码。
Thanks!
谢谢!
采纳答案by Nicola Musatti
In Python code each level of indentation mustcontain exactly the same sequence of spaces and tabs for each line that is indented to at least that level.
在 Python 代码中,每个缩进级别必须包含与至少缩进到该级别的每一行完全相同的空格和制表符序列。
The best and most simple way to achieve this is to only use spaces, always in the same number, for each indentation level. The suggested number is four.
实现这一点的最好和最简单的方法是只为每个缩进级别使用空格,始终使用相同的数字。建议的数字是四。
Many editors can be configured to replace tabs with a given number of spaces.
许多编辑器可以配置为用给定数量的空格替换制表符。