How to indent code in Emacs with python-mode? - python

I am running Emacs with python-mode.el for coding in Python. I hope to learn how to make a region of code indented well automatically.
Following code is not indented well.
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
If I select the region, and hit M-x indent-region, it becomes totally wrong:
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
The ideal should be:
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
Why does M-x indent-region incorrectly understand the indent
relation between lines of code? Is it because my code is ambiguous?
What should I do then?
Thanks.

The problem is that emacs has no way of knowing where you want an if-block to end. Both your desired code and the code indent-region produces are valid python. In C-like languages this isn't a problem due to the braces deciding the length of blocks. For python, since emacs can't know for sure it assumes each line of code is still part of the previous block.
You might want to look at python-indent-left (bound to "C-c <") and python-indent-right ("C-c >"). To fix your example, you'd highlight everything apart from the first line, and run python-indent-left.

As said, in Python you can't auto-indent larger sections reliably.
However, there is a way to speed up doing it line by line. This is in use here:
(defun indent-and-forward ()
"Indent current line and go forward one line. "
(interactive "*")
(if (empty-line-p)
(fixup-whitespace)
(indent-according-to-mode))
(if (eobp)
(newline-and-indent)
(forward-line 1))
(back-to-indentation))
BTW it should work with other modes too, not just Python. Keys here are
(global-set-key [(super i)] 'indent-and-forward)
This keys pressed, you may travel large parts - just keep an eye it still does what you want. If not - use TAB-key for just this line and proceed with the next one.

Related

problem with Python indentation with simple code, if, else

When I hit enter after typing else: to move to the next line, it gives error indentation, no matter how I align it, i tried 4 spaces, everything still not working. I made sure that else is aligned perfectly with if as in the book, but still error.
Can someone please explain to me how indentation works? I'm using Python 2.7
Code:
if x%2 == 0:
print "Even"
else:
print "Odd"
print "done with conditional"
Assume as if python has an reverse hierarchical structure like an inverted traingle.
when ever you want to write function/loop/conditions in 1st level, we write whatever the code we are writing inside those three sections in 2nd level.
In your code "if" and "else" comes in 1st level. so dont give any spaces infront of these.
print statements inside 'if' and 'else' should have same tab space or white space as they come under 2nd level.
outer print statement will not have a space because it again comes to 1st level.
Hope this helps
In Python console, everything works fine with a positive number of spaces, or a Tab;
so any of these example will work:
if True:
print('Hello')
else:
print('Not hello')
if True:
print('Hello')
else:
print('Not hello')
if True:
print('Hello')
else:
print('Not hello')
EDIT
For searching where is the problem, try this answer; call
python -m tabnanny <your_script>.py
and you will get an output telling you which lines are the problem.
Example output:
'test.py': Indentation Error: unindent does not match any outer indentation level (<tokenize>, line 12)

Avoiding indentation in Python else and elif

In writing a Python code in Wing or IDLE, inside the if condition, we must have an indentation for each line. But the else and the elif part must be aligned with if. However when I type else: or elif: in a newline, it has an indentation and I must remove it manually. I have seen it in several IDEs like Wing.
Is there any way to avoid indentation for else and elif? Indeed I need this:
if (condition):
do this
else:
do this
But when I type it, it's like:
if (condition)
do this
else:
do this
You seem to believe the editor will somehow know how many lines the indented suites of code should have.
It would be easy to make editors "outdent" if those suites were all one line, but a cursory inspection of any moderately complex Python program will show that is clearly not the case.
Which means it's up to us to indicate the end by outdenting manually, usually with SHIFT-TAB, at the end of the clause.

python else syntax error

This Python script to process a string is suffering from Syntax error in line 24, else:.
Any ideas as to what it might be?
j=raw_input("Enter a string: ")
import os
def addtoClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
def parse(string):
result=""
lineList=string.split("\n")
for i in range(len(lineList)):
h=lineList[i].split("#")
if len(h)<2:
continue
if len(h)>2:
count=0
for x in range(len(h)):
if x==len(h)-1:
continue
re0=count+len(h[x])+(x*1)
re1=i+1
re3=str(re1)+"-"+str(re0)
result+=str(re3)+", "
count+=len(h[x])
else:
re0=len(h[0])
re1=i+1
re3=str(re1)+"-"+str(re0)
result+=str(re3)+", "
result =result[:-2]
addtoClipBoard(result)
print result
parse(j)
It's probably a problem with the indentation level of the else, make sure to align it at the same level of the corresponding if. Please use a good IDE or text editor to help you catch this kind of errors. As it is, it's nearly impossible to se what you intended to do with the code.
Python uses indentation to define what is inside of a block of code (for,if,elif,while,with) You want to keep everything at the same indentation within the block and then go back a level to write your else statement
Also it doesn't look like your else: statement even belongs there, there is no if statement preceding it that would imply the need for an else are you sure that else is what you want?
Here is my suggestion without knowledge of what you want your code to do, based on observation of patterns
for x in range(len(h)):
if x==len(h)-1:
continue
if (###My condition goes here###):
re0=count+len(h[x])+(x*1)
re1=i+1
re3=str(re1)+"-"+str(re0)
result+=str(re3)+", "
count+=len(h[x])
else:
re0=len(h[0])
re1=i+1
re3=str(re1)+"-"+str(re0)
result+=str(re3)+", "
result =result[:-2]
addtoClipBoard(result)
print result
parse(j)

Python unexpected indentaton error main()

I have no idea how to fix this. I've tried retyping the program.
I get an unexpected indentation error for the last main function.
resident = 81
nonresident = 162
def main():
# initialize counters and total tuition
resident_counter = 0
nonresident_counter = 0
total_tuition = 0
print("Name \tCode\tCredits\tTuition")
print
try:
# open the data file
infile = open('enroll.txt', 'r')
# read the first value from the file
student_name = infile.readline()
# continue reading from file until the end
while student_name != '':
# strip the new line character and print the student's name
student_name = student_name.rstrip('\n')
print(student_name, end='\t')
# read the code type, strip the new line, and print it
code = infile.readline()
code = code_type.rstrip('\n')
print(code_type, end='\t')
# read the number of credits, strip the new line, and print it
credits = infile.readline()
credits = int(credits)
print(format(credits, '3.0f'), end='\t')
# check the room type and compute the rental amount due
# increment the appropriate counter
if code_type == "R" or room_type == "r":
payment_due = credits * resident
resident_counter += 1
elif code_type == "N" or room_type == "n":
payment_due = credits * nonresident
nonresident_counter += 1
elif code_type != "R" or code_type != "r" or code_type != "N" or code_type != "n":
payment_due = 0
# accumulate the total room rent
tuition += payment_due
# print the appropriate detail line
if payment_due == 0:
print('invalid code')
else:
print('$', format(tuition, '8,.2f'))
# get the next studen't name
student_name = infile.readline()
# close the input file
infile.close()
# print the counters and payment total amount
print
print('total number of resident students: ', resident_counter)
print('total number of nonresident: ', nonresident_counter)
print
print('total students: ', end='')
print('$', format(tuition, ',.2f'))
# execute the main function
main()
You don't have an except clause to match the try.
Despite what everyone else is saying, if you have a try, you don't have to have an except: you must have an except or a finally.
That may seem nitpicky, but it's pretty plausible that the code you were copying was actually using finally (e.g., to make sure some cleanup code always gets run—given that it's doing C-/Java-style manual cleanup).
At any rate, adding an except clause is not the right answer. If you're actually looking to handle or ignore errors, then yes, add an except clause. If you're looking for somewhere to add cleanup code that gets run even on an error, add a finally clause instead. If you don't want either, just remove the try line.
you are mixing room type and code type in your if else statement. You are also missing your except statement. This should be your last two lines before calling main.
It should read:
except IOError:
print('an error occurred trying to open or read enroll.txt')
You are using the try statement. It means that you MUST have an except block
try:
some code
except:
pass
It is simplification, but will solve your problem.
The other answers have (correctly) pointed out that you need to have an except to go with your try: (or not use a try altogether). I'm going to try to explain how you could go about debugging similar issues in the future, since these can be very annoying to debug.
Your error message most likely looked something like this:
File "test.py", line 74
main()
^
IndentationError: unexpected unindent
At first glance, that doesn't seem too helpful, and it's definitely not very clear.
The fact that you "tried retyping the program" sounds like you saw IndentationError and thought, "this means my indents are messed up; maybe there's an extra space somewhere or a mixture of spaces and tabs or something." Those are definitely valid IndentationErrors, and retyping the program would fix them, if you didn't make the same typing error again. (On a side note, I believe that any text editor worth using will have the option to convert all of your tabs to 4 spaces to avoid a lot of indent headaches.)
However, looking at the error text, we see "unexpected unindent". To me this sounds kind of like gibberish, but what it means is that, when python was looking at your program, it ran into a line that it thought should be indented more than it was. (Unfortunately, this same kind of error can be called "unexpected unindent" or "expected an indented block", and it can even show up as a plain old SyntaxError: invalid syntax.)
So knowing what the error message means, we can look back through the code to see why python thought that main() was indented strangely- though we have to be careful because python isn't always right about where the problem actually is. Here would be something like my thought process:
There's a function definition right before main() is called, maybe that function wants main() to be inside it? No that isn't it, because python only really expects at least one line of code after a function definition.
What else "wants" code to be indented after it? if, elif, and while all do, but all of them have code after them as well.
The only other "indenter" is try:. At this point, you either know that try: needs to be followed by a except: or a finally: and you fix it, or you don't. If you don't know that, than you could still see that try: is followed by an indented block, and, given that it is a likely culprit for you error, decide that it's time to look up what try: does.
Well I hope that helps troubleshooting problems like this in the future, and check out abarnert's answer and my link for more on handling errors with try/except/else/finally statements.
As noted by LevLevitsky and david above, there's no except clause. The easiest way to solve this is adding a line like the following to your code before the call to main:
except: pass
The second comment I have to your code is more stylistic, but you could add the following, before your call to main and after the except clause:
if __name__ == '__main__':
main()
Besides the missing except or finally or the extra try... If you have a look at the edited code, you can see the strangely mixed color blocks in the indentation. It means that you are mixing tabs and spaces which can lead to problems like bad indentation. The reason is that the interpreter thinks differently about how the tabs should be interpreted (for example 8 vs. 4 column tab stops).
You must not mix tabs and spaces for the indentation. I personally would recommend to use only the spaces. Any decent editor is capable to expand TAB key to spaces and/or Untabify the existing mixture of tabs and spaces.

Learning Python else syntax error

Hi I am learning python by doing the practice problems for Open course at MIT 6.00 Intro to Computer Science.
I am Trying to do practice problem 1 part 2 create a recursive function to count the instance of key in target. My code so far...
from string import *
def countSubStringMatchRecursive (target, key,x,s):
if (find(target,key)==find(target,key,s)) and (find(target,key)==find(target,key,(find(target,key)))):#if first and last
return (1)
elif (find(target,key)==find(target,key,s))and (find(target,key)!=find(target,key,(find(target,key)))):#if first but not last
x=1
s= find(target,key)
return (countSubStringMatchRecursive(target,key,s,x)
elif (find(target,key,s))==-1 and (find(target,key)!=find(target,key,s)):#if last but not first
return (x+1)
elif:(find(target,key,s))!=-1 and (find(target,key)!=find(target,key,s)):#if not last and not first
x=x+1
s= find(target,key,s)
return (countSubStringMatchRecursive(target,key,s,x)
I getting a syntax error at line 8. I would just like to know what I did wrong there. Dont worry about the other mistakes I should be able to get those sorted out. I just Stuck on this. Thanks.
You're missing a closing parenthesis in line 8 and in the last line. Actually, the corresponding opening parenthesis is unnecessary, you might as well rewrite those line as this:
return countSubStringMatchRecursive(target,key,s,x)
Also, as pointed by #rodion in the comments, the last elif has a misplaced :, remove the one right before the opening parenthesis.
And talking about parenthesis: in Python you don't have to place the conditions of an if ... elif ... else statement inside parenthesis, you should drop them.
You have an unmatched paren on line 8. Add another close paren at the end
return (countSubStringMatchRecursive(target,key,s,x))
Same thing applies to your final return statement.
You have a mismatched parenthesis. Remove the ( near the beginning of the line or add a ) to the end.
return (countSubStringMatchRecursive(target,key,s,x)
You're missing a closing ). But you didn't need the opening one either; the syntax of a return statement is return <expr>, so anything that's a valid expression can directly follow return. Anything that's a valid expression is still a valid expression meaning exactly the same thing if you surround it with parentheses, but mostly that just adds noise when it's not needed.
There are several problems:
You are missing a closing paren on line 8
the body of your function needs to be indented. (update: has been fixed)
Also, your last elif has a : right after it, that needs to be
deleted. I.e.,
elif: expression:
should be
elif expression:
Your last statement is also missing a closing paren
return (countSubStringMatchRecursive(target,key,s,x)
should be
return (countSubStringMatchRecursive(target,key,s,x))
Finally #drewk's recommendation about taking a look at the PEP 8 is a good one. I periodically go back and review it myself.
Also, you have a number of unnecessary ()s .. they don't do any harm, but they are not needed and may reduce the readability of your code.

Categories