I was progressing through the exercises on Zed Shaw's Learn Python 3 the Hard Way but then on Exercise 6, I encountered a syntax error, which I am not able to figure out. I tried searching on Google, StackOverflow (other posts), but none of the solutions mentioned work for me.
The snippet of the code that is throwing this error is:
types_of_people = 10
x = f"There are {types_of_people} types of people."
print(x)
I am using Visual Studio Code 1.38.1, with Python 3.7.4 64-bit on macOS Mojave 10.14.6.
Surprisingly, I executed the code three different ways, and two of the methods showed the same error, but a third method actually executed the code successfully. I am trying to understand why VSCode is not able to execute the python script. Any help would be highly appreciated.
Method 1
Used the standard way to execute python script in VSCode:
This method gave the SyntaxError. The error output is:
[Running] python -u "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py"
File "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py", line 2
x = f"There are {types_of_people} types of people."
SyntaxError: invalid syntax
[Done] exited with code=1 in 0.035 seconds
Screenshot 1 shows the error in VS Code.

Method 2
Used "Run Python file in terminal" option in VS Code.
This method executed the script successfully, and the output generated is:
KENMACC02XG4AEJHD2:Learn-Programminge139177$/usr/local/bin/python3/Users/e139177/Documents/Programming/Learn-Programming/Exercise6.py
There are 10 types of people.
KENMACC02XG4AEJHD2:Learn-Programming e139177$
Screenshot 2 shows the successfully executed script in VS Code terminal.

Method 3
Used MacOS terminal to directly execute the python script, without using VSCode.
This method also gave the same SyntaxError. The error output is:
KENMACC02XG4AEJHD2:Learn-Programming e139177$ python Exercise6.py
File "Exercise6.py", line 2
x = f"There are {types_of_people} types of people."
^
SyntaxError: invalid syntax
KENMACC02XG4AEJHD2:Learn-Programming e139177$
Screenshot 3 shows the successfully executed script in VS Code terminal.

I am not sure why the script executes successfully when run within the VSCode terminal, but it does not do so when executed using VSCode's "Run" command, or when executed within the macOS terminal directly.


import sys; print(sys.version), and you'll probably find that it's not actually 3.7.4.