0

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

  • For an exercise, I created a python script in VSCode that tells the user in what year they will turn 100 years old. Below is the code:
# 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) + "!.")
  • The code is supposed to ask user for name, age then calculate and print out the year the user will be 100 years of age.

  • When I ran it on VSCode, I got the error message below:

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
  • I believe the problem may lie in the VSCode python configuration having tested the same code in IDLE where it ran perfectly.

  • So far I have installed python 3.8 (previously had 3.7 installed) with no effect.

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.

1
  • The problem is in the first line of /Users/me/Documents/Education/phyton/exercises/math.py Commented May 18, 2020 at 15:26

1 Answer 1

3

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.

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

2 Comments

thank you so much! I simply deleted the math.py file. I imagine I have to be careful with the names I give to python scripts then to avoid these type of clashes.
I'm glad this was useful.

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.