Syntax error for simple script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Syntax error for simple script (/thread-3802.html) |
Syntax error for simple script - pstein - Jun-26-2017 Sorry for this newbie question: I installed Python v3.6.1 on win 7. Afterwards I tried to execute the following simple python script from webpage www.pythonforbeginners.com/code-snippets-source-code/date-and-time-script/: from datetime import datetime now = datetime.now() mm = str(now.month) dd = str(now.day) yyyy = str(now.year) hour = str(now.hour) mi = str(now.minute) ss = str(now.second) print mm + "/" + dd + "/" + yyyy + " " + hour + ":" + mi + ":" + ssWhen executing D:\tools\python\python.exe dateParser.py it yields a syntax error: File "dateParser.py", line 17 print mm + "/" + dd + "/" + yyyy + " " + hour + ":" + mi + ":" + ss ^ SyntaxError: Missing parentheses in call to 'print' Whats wrong? Thank you Peter RE: Syntax error for simple script - ichabod801 - Jun-26-2017 Well, as the error says, you are missing parentheses. The web page you are using must be written in Python 2, whereas you have installed Python 3. There were several changes between two and three, one that commonly trips up new people is that print changed from a statement (not needing parens) to a function (needing parens). This change should fix the problem: print(mm + "/" + dd + "/" + yyyy + " " + hour + ":" + mi + ":" + ss) RE: Syntax error for simple script - snippsat - Jun-26-2017 When using 3.6 can use f-string and also get rid of all unnecessary + and str() convert.from datetime import datetime now = datetime.now() mm = now.month dd = now.day yyyy = now.year hour = now.hour mi = now.minute ss = now.second print(f'{mm}/{dd}/{yyyy} {hour}:{mi}:{ss}')
|