python - IndentionError: unexpected indent, in except statement -
i making python program checks if server if isn't tweet saying it's down. go on tweet when comes up.
but when run code error:
file "tweet_bot.py", line 31 textfile = open('/root/documents/server_check.txt','w') ^ indentationerror: unexpected indent
my code broken section below:
try : response = urlopen( url ) except httperror, e: tweet_text = "raspberry pi server down!" textfile = open('/root/documents/server_check.txt','w') textfile.write("down") textfile.close() except urlerror, e: tweet_text = "raspberry pi server 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 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 , tweet_text > 0: api.update_status(status=tweet_text) else: pass
you mixing tabs , spaces:
>>> pprint import pprint >>> pprint(''' ... tweet_text = "raspberry pi server down!" ... textfile = open('/root/documents/server_check.txt','w') ... '''.splitlines()) ['', ' tweet_text = "raspberry pi server down!"', "\ttextfile = open('/root/documents/server_check.txt','w')"]
note \t
@ start of second line, first has 4 spaces instead.
python expands tabs next 8th column, more 4 spaces in first line. second line indented two indentation levels, while first indented 1 level.
the python style guide, pep 8 recommends use spaces only:
spaces preferred indentation method.
and
python 2 code indented mixture of tabs , spaces should converted using spaces exclusively.
as getting tabs configured correctly , not accidentally muck indentation mixing in few spaces hard.
configure editor convert tabs spaces when edit; way can still use tab keyboard key without falling trap.
Comments
Post a Comment