Why am I getting unexpected indent error on an empty line? - python

This is a cog file for a discord.py rewrite based bot. As you can see, it is completely empty except for the class and the setup, but still I'm recieving an unexpected indenterror. Does anyone know what's causing this error?

Its no real error, I guess.
Only a linting error.
Deleting the line may do the trick.
Some linteres require you to have a max of two empty lines between some statements.

Errors occurs when part of your code is indented with spaces, while other parts are indented with tabs. The best practice is to use four spaces instead of tabs.

Related

How should I handle this Unexpected Indent?

When I tried to run this code, it gives me the error message of line21 UnexpectedIndent. How do I go about this? Thanks.
for filepath in matches:
with open (filepath,'rt') as mytext:
mytext=mytext.read()
print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext) #This line just aims to diagnose the problem.
matchOrg=re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext)[0]
capturedorgs.append(matchOrg)
When I got rid of the print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext), the error message is MatchOrg... list out of range.
Going to meeting. Will check back all replied after 4p.
The line mytext=... is indented with a tab, and the rest is indented with spaces. Use either tabs or spaces, but don't mix them. The use of spaces is encouraged over the use of tabs.
In your text editor use the option to show all characters. Make sure indentations are consistently represented either with spaces or tabs and not a mix of both.
At times when you copy snippets of code from other sources, the indentation from your source and the other may differ causing the python interpreter to throw the unexpected indentation error as below

Vexing Python syntax error

I am writing a python script using version 2.7.3. In the script a line is
toolsDir = 'tools/'
When I run this in terminal I get SyntaxError: invalid syntax on the last character in the string 'r'. I've tried renaming the string, using " as opposed to '. If I actually go into python via bash and declare the string in one line and print it I get no error.
I checked the encoding via file -i update.py and I get text/x-python; charset=us-ascii
I have used TextWrangler, nano and LeafPad as the text editors.
I have a feeling it may be something with the encoding of one of the editors. I have had this script run before without any errors.
Any advice would be greatly appreciated.
The string is 'tools/'. toolsDir is a variable. You're free to use different terminology, of course, but you'll end up confusing people trying to help you. The only r in that line is the last character of the variable name, so I assume that's the location of the error.
Most likely you've managed to introduce a fixed-width space (character code 0xA0) instead of an ordinary space. Try deleting SP=SP (all three characters) and retyping them.
Try running the code through pylint.
You probably have a syntax error on a nearby line before this one. Try commenting this line out and see if the error moves.
You might have a whitespace error, don't forget whitespace counts in python. If you've mixed tabs and spaces anywhere in your file it can throw the syntax checker off by several lines.
If you copied and pasted lines into this from any other source you may have copied whitespace in that doesn't fit with whichever convention you used.
The error was, of course, a silly one.
In one of my imports I use try: without closing or catching the error condition. pylint did not catch this and the error message did not indicate this.
If someone in the future has this triple check all opening code for syntax errors.

Python: getting an error for function, Unindented error

I'm new to Python, and I'm trying to make my string change 'abc' to 'def' and in short I keep getting,"unindented does not match any other indentation level." Therefore, this means what is wrong with my function?
def changes(x):
if 'a' in sent:
x=sent.replace('a','d')
if 'b' in x:
y=x.replace('b','e')
if 'c' in y:
z=y.replace('c','f')
print(z)
sent=print(input('Enter a sentence:'))
changes(x)
Just check your indentation. You cannot write anything anywhere like in other languages.
Read this short guide.
I have had, and still have, lots of these errors. What I found really useful is to turn on so I can see invisibles or whitespace. When copying fragments on code from the web, or changing between using the spacebar and the tabulator for creating indentation this can create problems with indentation
Check the if 'a' in sent, and the line below. Seems to use fewer spaces than the subsequent cases.

Sumbline Text 2, Eclipse all acting up, IndentationError

I'm having a problem with python interpreters. It happened 2nd time in recent months and is very frustrating. This is my code:
When I run this, the interpreter raises an error:
else:
^
IndentationError: unindent does not match any outer indentation level
same is occuring on Eclipse.
I typied code by hand, no copy/pase, tried to fix it many times and the error is not subsiding. Although the code is 100% corrent. When I remove else: pass part it even sometimes raises open_odds is not defined error.
Have you guys had similar problems? Even python IDLE is now acting up on the same code line, the same error. I can no longer work ;/.
You are mixing tabs and spaces. Don't do that.
Run your script with python -tt then fix the errors that finds.
Next, configure your editor to only use spaces:
View -> Indentation -> Indent Using Spaces
View -> Indentation -> Tab Width: 4
View -> Indentation -> Convert Indentation to Spaces
and perhaps set that as the default for Python. See Indentation settings in the Sublime Text 2 documentation.

Problems with Nested Functions

THIS TURNED OUT TO BE A SYNTAX ERROR ON MY PART A LINE EARLIER IN THE CODE.
Hello, I'm having some trouble with a nested function I wrote in python. Here is the relevant code.
device = "/dev/sr0"
def burn():
global device
burnaudiotrack(device)
createiso(device)
burntrack2(device)
I'm confused, because every time I try to run the script, python returns this:
File "./install.py", line 72
burnaudiotrack(device)
^
SyntaxError: invalid syntax
I've nested functions before, and done so in a similar manner. I feel like I'm missing something fairly obvious here, but I can't pinpoint it. Thank you for your help/suggestions!.
EDIT:
Full code: (I tried to just post relevant info in the original)
http://dpaste.com/hold/291347/
It's a tad messy, and there may be other errors, but this one is vexing me at the moment.
You are missing a close parenthesis on line 61.
Looks like the quote and paren at the end of the line are swapped.
speed = raw_input("Recomended(4);Default(8))"
should be
speed = raw_input("Recomended(4);Default(8)")
The code you have pasted into your question appears to have tabs as well as spaces. You should (according to PEP-8) always use spaces for indenting in Python. Check your text editor settings.
What's probably happened is you have some mix of tabs and spaces that looks correct in your editor, but is being interpreted differently by the Python compiler. The Python compiler sees a different inconsistent indenting, and throws a SyntaxError.
Update: As another answer points out, you are missing a closing parenthesis on a line of code you didn't show in your original question. Nevertheless, my comments about tabs in your source still hold.

Categories