Tim
Tim

Reputation: 11

Python code runs interactively but not when run as script

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.

Upvotes: 1

Views: 49

Answers (1)

Tomthepythonist
Tomthepythonist

Reputation: 81

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.

Upvotes: 1

Related Questions