Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmajesticcat123
3,020 PointsOdd syntax error when running Python file in Terminal on Mac?
So, following a tutorial from this site, I made a Python file into an executable file. However, when I run it in Terminal, I got the following odd syntax error.
[my filename and path were here] ; exit;
Enter the observation:
This works?
Traceback (most recent call last):
File "[my filename and path were here]", line 7, in <module>
observation = input("Enter the observation:\n")
File "<string>", line 1
This works?
^
SyntaxError: invalid syntax
[Process completed]
However, when I ran my Python file in the Python Shell in IDLE, it worked just fine (and still does). My Python code is as follows. I would appreciate any help!
from datetime import *
date = datetime.now().date()
notebook = open(date.strftime("%d %B %Y (%A) Observations.txt"), "a")
while True:
observation = input("Enter the observation:\n")
time = datetime.now().time()
time = time.strftime("%H:%M")
if observation == "STOP":
break
if observation == "SAVE":
notebook.close()
notebook = open(date.strftime("%d %B %Y (%A) Observations.txt"), "a")
continue
notebook.write("{} {}\n".format(time, observation))
notebook.close()
2 Answers
Jeff Muday
Treehouse Moderator 28,720 PointsThe syntax you are using works in Python 3.x, but you might be using Python 2.x depending on the age of your Mac.
Python 2 uses
raw_input()
rather than input()
majesticcat123
3,020 PointsOK then, I updated everything and found out... I needed to put them in with "quotes" to show it was a string, otherwise, it treated it like a variable name.
majesticcat123
3,020 Pointsmajesticcat123
3,020 PointsI have been getting messages saying to update things like pip, so I think you're right. I'll look into that!