-4

I am a beginner in python. Python version which I am using is 3.8. Picture in this link:picture. I am trying to run the command below:

name = input("Please enter your name: ")
print(f"Hello, {name}")

But, python gives me a syntax error when I am running the command.

File "/Users/Belekkanybekov/Desktop/python_work/Python.Chapter7/greeter.py", line 6
  print(f"Hello, {name}")
                       ^

Syntax Error: invalid syntax
  • 3
    What is the exact error? The code you show is fine, assuming you are using Python 3.6 or later. The error message suggests you are using an older version of Python. – chepner Jun 11 '20 at 15:23
  • 2
    Hello and welcome to StackOverflow and Python! Would you mind editing your post and writing out the error instead of linking to it? Also, which version of Python are you running? – Daniel Walker Jun 11 '20 at 15:23
  • 1
    The syntax error isn't on the input line, it's on the print line. It's because you're using an old version of Python without f-strings. – Barmar Jun 11 '20 at 15:27
  • 2
    See https://stackoverflow.com/questions/42126794/python-3-returns-invalid-syntax-when-trying-to-perform-string-interpolation – Barmar Jun 11 '20 at 15:28
  • You're not using Python 3.8. – ChrisGPT was on strike Jun 13 '20 at 00:35

1 Answers1

1

A SyntaxError: invalid syntax error occurs when you try to run your Python code with the wrong Python interpreter (python instead of python3).

$ python
Python 2.7.17  
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> name = input("Please enter your name: ")
Please enter your name: "Becky"
>>> print(f"Hello, {name}")
  File "", line 1
    print(f"Hello, {name}")
                         ^
SyntaxError: invalid syntax
>>>

Your Python version is 3.8. Your code runs correctly using the python3 interpreter which can be started with python3.

To run greeter.py with Python 3:

python3 '/Users/Belekkanybekov/Desktop/python_work/Python.Chapter7/greeter.py'
karel
  • 5,489
  • 46
  • 45
  • 50