-3

I am fairly new to Python and I am using VS Code on Linux. I have a function that reads a file in, and decodes some data. The function has two try: except: statements in it. The second set in the code is showing as unreachable and I cant figure out why. Can someone explain to me why this is happening?

    def run(self):
    self.connectPipe()
    while True:
        try:
            ans = self.server.readFile(self.tid,self.fid, 0, 1024)
        except Exception, e: 
            pass
        else:
            try:
                global LastDataSent
                if ans != LastDataSent:
                    sys.stdout.write(ans)
                    sys.stdout.flush()
                else:
                    # Don't echo what I sent, and clear it up
                    LastDataSent = ''
                # Just in case this got out of sync, i'm cleaning it up if there are more than 10 chars, 
                # it will give false positives tho.. we should find a better way to handle this.
                if LastDataSent > 10:
                    LastDataSent = ''
            except:
                pass
Steve Weaver
  • 342
  • 1
  • 4
  • 18
  • 7
    Please [edit] your question to fix your code's indentation. – Brian61354270 Aug 11 '23 at 00:40
  • 1
    Using `else` after an `except` ps more of an advanced python concept. Please explain in more detail what you're trying to accomplish – OneCricketeer Aug 11 '23 at 00:50
  • 1
    [ask] and [mre] – Julien Aug 11 '23 at 00:58
  • 2
    Hint: If you use spaces (not tabs) to indent your code, the code doesn't get messed up when moving or copying code or code snippets between different platforms; e.g. into StackOverflow questions. This is particularly important for Python ... where indentation is syntactically significant. – Stephen C Aug 11 '23 at 01:13
  • 2
    Please use consistent indentation amounts, preferably 4 characters per level. Single-space indentation is unreadable. – Barmar Aug 11 '23 at 01:16
  • The second `except:` has to be at the same indentation level as the second `try:` – Barmar Aug 11 '23 at 01:17

1 Answers1

-1

I was able to figure it out. It was the indentation that was the cause. Although I was sure I had straightened it up before hand. I used F1 in VS code and used "Convert indentation to spaces" That corrected the code to what you see above. Thank you for your help!

Steve Weaver
  • 342
  • 1
  • 4
  • 18
  • As a reminder, please keep in mind that this is **not a discussion forum**. If a problem was caused by some simple typo or similar oversight, not by a lack of comprehension, it does not form a suitable question for Stack Overflow. Aside from that, don't edit the question to show corrected code (the **point** of questions here is to **show** a problem, so that the cause can be explained while making reference to an example **that actually causes** the problem); make sure questions show a [mre]. Finally, avoid [noise](https://meta.stackexchange.com/questions/2950) in answers. – Karl Knechtel Aug 11 '23 at 01:42