Looms
Looms

Reputation: 3

How do I resolve VSCode syntax error for python script that runs fine in IDLE?

I'm new to programming and learning python using VSCode as my IDE.

# gets user age and prints the year user will be 100 years old

import datetime

cDate = datetime.datetime.now()
cYear = int(cDate.year)

uName = input("Hi, what is your name?: ")
print ("Hello " + uName)
print ("I will tell you what year you will turn 100 years!")
uAge = int(input("How old are you?: "))

year100 = cYear - uAge + 100

print ("You will be 100 years old in the year " + str(year100) + "!.")
Traceback (most recent call last):
  File "/Users/me/Documents/Education/phyton/exercises/age in 100 years.py", line 3, in <module>
    import datetime
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/datetime.py", line 8, in <module>
    import math as _math
  File "/Users/me/Documents/Education/phyton/exercises/math.py", line 1
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
             ^
SyntaxError: invalid syntax

My system details include: - macOS Catalina - Python 2.7 and 3.8 installed

Please do not hesitate to ask if you have any other questions.

Upvotes: 0

Views: 197

Answers (1)

phrodod
phrodod

Reputation: 166

In the directory with your age.py file, you also have a math.py file. The datetime module (which you use by typing import datetime) is looking for the Python math module, math.py. But it's finding your math.py file first.

The issue is in that file, not in your age.py file. There's apparently a line that starts with Python 3.7.0... that isn't valid Python syntax. You should probably rename math.py to something like my_math.py, which should resolve the issue.

Upvotes: 3

Related Questions