OK, what am I doing wrong now?

if indata == "C":
                    ^
SyntaxError: invalid syntax

indata is a string. I don’t get it (the carrot is pointing to the colon)

[quote=“KenHorse, post:1, topic:9378”]
Sincerely , I cannot understand the context of the code a 100%, but I will try to give you a hint.


As you can see, the value of your statement change, depending on how you declare your variable.

Since, I don´t know what are the values of indata and “C”, and even what is the the nature of your project, I cannot give too much feedback.
But I suspect, the Syntax error is in the variables defined, or … these predicates are dependent of a loop or another function.
Hope it helps

In the mid

Sorry.

The context is I’m reading a serial port (pyserial)

while var==1:
  indata = str(ser.readline())  
 #check for "C" and exit loop
  if indata == "C":
    break

Does that help?

Ok, got it :smiley:
In my limited Knowledge, I tried to bring a solution. Hope it helps:


you need to add a counter in your loop, in this way, the loop will stop at some point. And regarding syntax, the problem was the str. I did it only with ser.readline().
Sincerely hope it helps.

Hi Ken,

Thanks for posting your code, but I’m confident that it isn’t the same
code that you are using. Your code looks like this:

while var==1:
    indata = str(ser.readline())  
    #check for "C" and exit loop
    if indata == "C":
        break

but that doesn’t give a SyntaxError. That is perfectly correct, legal
code (at least it is syntactically legal – it needs to have var
defined to actually work).

I am confident that the problem is that you are missing a closing
bracket on the indata line, like this:

while var==1:
    indata = str(ser.readline()   # oops, missing closing parenthesis
    #check for "C" and exit loop
    if indata == "C":
        break

because that gives the SyntaxError you report:

>>> while var==1:
...   indata = str(ser.readline()
...  #check for "C" and exit loop
...   if indata == "C":
  File "<stdin>", line 4
    if indata == "C":
                    ^
SyntaxError: invalid syntax

So, three lessons here:

  1. If you get a mysterious Syntax Error that otherwise doesn’t make
    sense, there is a good chance that the cause is on the previous line,
    due to missing brackets.

  2. When copying and pasting the code you are using, it helps to copy and
    paste the actual code you are using, and not a slightly modified
    version that accidentally fixes the fault you are reporting.

  3. And most importantly, I am bloody awesome grins

Sorry its been a long day and I need a pat on the back :slight_smile:

One other comment: if your intention is to have an infinite loop, you
don’t need the var==1 bit. You can just do this instead:

while True:
    # infinite loop that will go forever
    # or until you use break to exit

And it will be ever-so-slightly faster, if it matters.

Steven

Thank you! Yes, I did not post a line (as it was a print statement used for troubleshooting) that turned out to be the problem, with a missing “)”. I will remember your sage advice in the future.

Admittedly, my experience with Python is limited and most of that experience is with Python 2.7 and a LOT has changed. Thanks again for your help

As here you can see you defined indata == “c”
So “c” should be defined as C