-1

No matter how many times I indent my code, I keep getting this error. I even added exception handling and it still errors out.

import gps 
import os

session=gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE|gps.WATCH_NEWSTYLE)

while True:
      try:

            report = session.next()
            if report['class'] == 'TPV':
                if hsattr(report,'lat' and 'lon'):
                        latitude  = report.lat
                        latString = "lat:%f" % (latitude)
                        longitude = report.lon
                        longString = "lon:%f" % (longitude)
                        f = open("gsp.txt", "w")
                        f.write("%s,%s" %(latString,lonString))
                        f.close()
                        #os.system("java -jar gps.jar")
AndyG
  • 39,700
  • 8
  • 109
  • 143
soad666p
  • 11
  • 5
  • Why is the `java` tag needed? – devnull Apr 05 '14 at 08:12
  • 1
    Why do you have a `try` block? You're not handling any errors. A `try` without an `except` or `finally` is both useless and invalid. – user2357112 Apr 05 '14 at 08:15
  • While we're at it, these are your next two bugs: You've written `hsattr` instead of `hasattr`, and [`and` doesn't work the way you're trying to use it](http://stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah). – user2357112 Apr 05 '14 at 08:20
  • sorry about java tag, the try block have fix that issues before posting the qustions but never update the code. – soad666p Apr 05 '14 at 15:50

1 Answers1

1

Besides the errors already mentioned in the comments (hsattr, 'lat' and 'lon, etc), the indentation problem arises from latitude = report.lat and the following lines being indented twice.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • That actually shouldn't matter. Python doesn't care how big your indents are or whether they're the same size as each other. You just need to make sure the unindents match the corresponding indents. – user2357112 Apr 06 '14 at 01:14
  • @user2357112 You are right (and I am quite surprised). Unfortunately SO doesn't let me delete this answer, as it has been accepted, wrong as it be. – Hyperboreus Apr 06 '14 at 01:56