0

Our lecturer gave us some code to help us with an assignment and when I try to run it I get a syntax error. I haven't touched the code at all and it's meant to run without us having to change anything. This is half the code that we were given:

class Flusher(MessageProc):
        def main(self):
            super().main()
            print('before start message')
            self.receive(
                Message(
                    'start',
                    action=self.flush))
            print('after start message')
            self.receive(
                Message(
                    ANY,
                    action=lambda data:print('The first thing in the queue after the flush is', data)))
        def flush(self, *args):
            self.receive(
                Message(
                    ANY,
                    action=self.flush), # recursively call the flush method
                TimeOut(
                    0,
                    action=lambda: None)) # when no more messages return

And the line that throws the exception is:

action=lambda data:print('The first thing in the queue after the flush is', data)

This is my first time using Python so please can someone explain what's wrong and what I should do to fix it.

EDIT: The error trace is:

File "./demo_timeout.py", line 18
    action=lambda data:print('The first thing in the queue after the flush is' + data)))
                           ^
SyntaxError: invalid syntax
7
  • Post the full error trace... Commented Aug 12, 2016 at 4:18
  • 1
    Are you on Python 2, or 3? Are you sure? Commented Aug 12, 2016 at 4:18
  • @user2357112 I'm using python 3 Commented Aug 12, 2016 at 4:19
  • 3
    The evidence strongly suggests you're mistaken about that. Commented Aug 12, 2016 at 4:22
  • So, you think I'm using python 2? Commented Aug 12, 2016 at 4:23

1 Answer 1

1

That code is Python 3, and you're attempting to run it using Python 2. If you instead run it in Python 3, you should be good.

How you go about switching to 3 depends entirely on your execution environment, but there's a decent chance you already have a command available called python3; if so, you should use that instead of python to run the supplied code.

If you don't already have a python3, then you need to install it, however you go about installing software on your system.

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

1 Comment

Thanks, that was really helpful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.