Python Forum
SyntaxError: unexpected EOF while parsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: unexpected EOF while parsing
#1
I'm using Python 3.6.6 and I don't understand what I need to do to correct this code error.

Here's my code:
# First import the library
import pyrealsense2 as rs

try:
    # Create a context object. This object owns the handles to all connected realsense devices
    pipeline = rs.pipeline()
    pipeline.start()

    while True:
        # Create a pipeline object. This object configures the streaming camera and owns it's handle
        frames = pipeline.wait_for_frames()
        depth = frames.get_depth_frame()
        if not depth: continue

        # Print a simple text-based representation of the image, by breaking it into 10x20 pixel regions and approximating the coverage of pixels within one meter
        coverage = [0]*64
        for y in xrange(480):
            for x in xrange(640):
                dist = depth.get_distance(x, y)
                if 0 < dist and dist < 1:
                    coverage[x/10] += 1

            if y%20 is 19:
                line = ""
                for c in coverage:
                    line += " .:nhBXWW"[c/25]
                coverage = [0]*64
                print(line)
As I read about this error I find this- The SyntaxError: unexpected EOF while parsing means that the end of your source code was reached before all code blocks were completed. A code block starts with a statement like for i in range(100): and requires at least one line afterwards that contains code that should be in it.

The for loops appear to me to have a line of code after them. Maybe someone can explain this for me?

Thanks in advance.
Reply
#2
You used try without either except and/or finally. In general, placing large blocks of code under try clause reduce readability - you would either want to place specific line(s) that may cause exceptions under try - or arrange your code in a function and call this function under try clause
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Yes, @volcano63 said it clearly. Put the specific jobs into functions. That way a whole code block will be a function call and you won't forget to put except statement after try.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unexpected EOF while parsing dawid294 1 403 Jan-03-2024, 04:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,173 Jul-13-2020, 07:05 PM
Last Post: snippsat
  unexpected EOF while parsing aaron92 3 3,529 Sep-11-2019, 10:01 PM
Last Post: aaron92
  unexpected EOF while parsing whatloop 3 8,639 Mar-09-2019, 06:59 PM
Last Post: whatloop
  pdb says "SyntaxError: unexpected EOF" on comment pwannh 1 3,068 Nov-29-2018, 04:17 PM
Last Post: nilamo
  unexpected output while parsing file anna 3 3,184 Apr-28-2018, 05:13 PM
Last Post: anna
  Unexpected Output after Running PArsing Script hotsea 7 5,058 Jan-10-2018, 09:55 AM
Last Post: hotsea
  SyntaxError: unexpected character after line continuation character Saka 2 18,550 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020