IndentionError: unexpected indent, in except statement
IndentionError: unexpected indent, in except statement
我正在制作一个 python 程序来检查我的服务器是否已启动,如果未启动,它会发推文说它已关闭。然后它会在恢复时继续发推文。
但是当我 运行 我的代码出现这个错误时:
File "Tweet_bot.py", line 31
textfile = open('/root/Documents/server_check.txt','w')
^
IndentationError: unexpected indent
我的破损部分代码如下:
try :
response = urlopen( url )
except HTTPError, e:
tweet_text = "Raspberry Pi server is DOWN!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("down")
textfile.close()
except URLError, e:
tweet_text = "Raspberry Pi server is DOWN!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("down")
textfile.close()
else :
html = response.read()
if textfile = "down":
tweet_text = "Raspberry Pi server is UP!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("up")
textfile.close()
if textfile = "up":
tweet_text = ""
pass
if len(tweet_text) <= 140 and tweet_text > 0:
api.update_status(status=tweet_text)
else:
pass
您混用了制表符和空格:
>>> from pprint import pprint
>>> pprint('''
... tweet_text = "Raspberry Pi server is DOWN!"
... textfile = open('/root/Documents/server_check.txt','w')
... '''.splitlines())
['',
' tweet_text = "Raspberry Pi server is DOWN!"',
"\ttextfile = open('/root/Documents/server_check.txt','w')"]
注意第二行开头的 \t
,但第一行有 4 个空格。
Python 将制表符扩展到下一个第 8 列,比第一行的 4 个空格多。因此,第二行缩进到 两个 缩进级别,而第一行仅缩进一个级别。
Python style guide, PEP 8推荐你只使用空格:
Spaces are the preferred indentation method.
和
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
因为正确配置选项卡并且不会因混入几个空格而意外弄乱缩进是很困难的。
将您的编辑器配置为在您编辑时将制表符转换为空格;这样你仍然可以使用 TAB 键盘键而不会落入这个陷阱。
我正在制作一个 python 程序来检查我的服务器是否已启动,如果未启动,它会发推文说它已关闭。然后它会在恢复时继续发推文。
但是当我 运行 我的代码出现这个错误时:
File "Tweet_bot.py", line 31
textfile = open('/root/Documents/server_check.txt','w')
^
IndentationError: unexpected indent
我的破损部分代码如下:
try :
response = urlopen( url )
except HTTPError, e:
tweet_text = "Raspberry Pi server is DOWN!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("down")
textfile.close()
except URLError, e:
tweet_text = "Raspberry Pi server is DOWN!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("down")
textfile.close()
else :
html = response.read()
if textfile = "down":
tweet_text = "Raspberry Pi server is UP!"
textfile = open('/root/Documents/server_check.txt','w')
textfile.write("up")
textfile.close()
if textfile = "up":
tweet_text = ""
pass
if len(tweet_text) <= 140 and tweet_text > 0:
api.update_status(status=tweet_text)
else:
pass
您混用了制表符和空格:
>>> from pprint import pprint
>>> pprint('''
... tweet_text = "Raspberry Pi server is DOWN!"
... textfile = open('/root/Documents/server_check.txt','w')
... '''.splitlines())
['',
' tweet_text = "Raspberry Pi server is DOWN!"',
"\ttextfile = open('/root/Documents/server_check.txt','w')"]
注意第二行开头的 \t
,但第一行有 4 个空格。
Python 将制表符扩展到下一个第 8 列,比第一行的 4 个空格多。因此,第二行缩进到 两个 缩进级别,而第一行仅缩进一个级别。
Python style guide, PEP 8推荐你只使用空格:
Spaces are the preferred indentation method.
和
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
因为正确配置选项卡并且不会因混入几个空格而意外弄乱缩进是很困难的。
将您的编辑器配置为在您编辑时将制表符转换为空格;这样你仍然可以使用 TAB 键盘键而不会落入这个陷阱。