1

I am new to python and have looked at similar questions but none seems to offer a solution to my simple case so I suspect I have made a basic error. I am using Python 2.7 on a Mac and on Ubuntu running on a Chromebook. When the project is complete I shall transfer it to a Raspberry Pi.

This snippet of code runs without a problem if I invoke the interpreter with the python call and type in the code.

switch = (int(time.strftime("%M"))%2
WhichOne = "Right","Left"
usbname = WhichOne[switch]

However, when I run the script containing this code fragment by typing ./project20160218.py or python project20160218.py

I obtain

 user@chrubuntu:~/Documents/Degree day project$ python project20160218.py
  File "project20160218.py", line 23
  WhichOne = "Right","Left"
       ^
  SyntaxError: invalid syntax

I would be very grateful for some guidance here.

Thanks.

1 Answer 1

1

You're missing a closing parenthesis in the first line just before %2:

switch = (int(time.strftime("%M")))%2
WhichOne = "Right","Left"
usbname = WhichOne[switch]

If you close that it works. Also I assume you're importing time elsewhere, otherwise that would be undefined and also cause issues.

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

3 Comments

Tom. Would you explain why? I see three opening and three closing brackets. In fact I could remove the outer pair I believe.
In the question there are 3 opening parentheses and 2 closing parentheses on that first line. That imbalance creates a syntax error when you try to set the WhichOne variable. It's worth noting that your example didn't run in the interpreter when I ran it myself until I fixed that, so if balancing the parentheses still doesn't fix your issue then please let us know.
You are right. I was looking at the code included with your answer not the code that I submitted. Thank you. I am surprised that the syntax error did not occur at the level of that statement rather than with the following line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.