-1

I have never done Python programming before but I have done a lot of .NET in C# There is a need to do some Python and was trying to learn how to throw exceptions in Python.

from __future__ import with_statement

import dbi
import timeit
import datetime
import win32com.client
import os
import datetime
import sys
import codecs
import subprocess
import time
import _winreg


def main():
    retCode=0
    now = datetime.datetime.now()
    print "-----------------------------------------------------"
    print "Start Executing the script", now
    print "-----------------------------------------------------"

    try:
        print "Testing Testing"

    except Exception, err:
        sys.stderr.write('ERROR: %s\n' % str(err))
        retCode=1

    if retCode==1:
        print "======================================"
        print "Errors occured. Please investigate. "
        print "======================================"
    else:
        print "======================================"
        print "No errors found"
        print "======================================"

    print "Retcode:", retCode
    sys.exit(retCode)
main()

Does anyobody know why the above code gives me this error ?

The error is: 
  File "c:\sample.py", line 28
    retCode=1
    ^
IndentationError: unexpected indent
1
  • I don't know the solution, but an improvement over the usage of the main() function would be to use if __name__ == '__main__':. Commented Sep 11, 2011 at 17:29

2 Answers 2

3

Check the indentation at retCode=1. If this line has 4 spaces but the line above a tab, it will look the same in your editor but python will complain.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I knew about the 4 spaces for intent. I had though a tab and then 4 spaces. I still don't get the whole concept of spaces in Python :)
1

In your original code, did you mix tabs and spaces? Make sure each line has the same number of spaces per indentation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.