Python3 Ex5 I don't understand why "print" is giving me a syntax error

No matter what I do, whenever I type Zed’s example from exercise 5 or copy and paste it directly, the first line of print code always comes up with a syntax error (in this case line 9). Whenever I delete this line, the next line will also be seen as a syntax error.

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print(f'Let's talk about {my_name}.'')
print(f"He's {my_height} inches tall.")
print(f"He's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")

# this line is tricky, try to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

I get this error:

[code]
line 9
print(f’Let’s talk about {my_name}.’’)
^
SyntaxError: invalid syntax

E0001[/code]

@krmanski

at first glance I notice print line does not have matching quotes you start with single and end with double

I have actually found isolating the code in the terminal and mucking about with it has given me a better appreciation for what may (or may not) be going on. If I isolate the piece in question and run it in the interpreter I get:

Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> my_name = 'Graham'
>>> print(f’Let’s talk about {my_name}.’’)
  File "<stdin>", line 1
    print(f’Let’s talk about {my_name}.’’)
                ^
SyntaxError: invalid character in identifier
>>> print(f"Let’s talk about {my_name}.")
Let’s talk about Graham

What I found by doing this is that the interpreter is getting two single quotes '' at the end and not a single double quote ". When I do that the SyntaxError flags the s after the Let' which tells me that Python believes this is the end of the print statement.

It doesn’t matter if you use single or double quotes except when you want to use a single quote within the print statement. For consistency I’d suggest always using double quotes.

>>> print(f'Lets talk about {my_name}.')
Lets talk about Graham.
>>> print(f'Let's talk about {my_name}.')
  File "<stdin>", line 1
    print(f'Let's talk about {my_name}.')
                ^
SyntaxError: invalid syntax
>>> print(f"Let's talk about {my_name}.")
Let's talk about Graham.
>>>

Sorry if this is a bit of a ramble, hopefully it explains what’s going on a bit better.

–Graham

This is common. You have Python 3.5 or 3.4 installed, but you need Python 3.6.

I’m having issues with this exercise too. Double checked my quotes and all seem to be correct. Any suggestions? I get the same error:

File “ex5.py”, line 9
print(f"Let’s talk about {my_name}.")

I commented out each single line, to try them all individually and each print statement gives an error.

my_name = 'Zed A. Shaw'
my_age = 34 # not a lie
my_height = 74 # inches 
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
 
print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")
 
# this line is tricky, try to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

Solved, I should have used the command python3.6 ex5.py instead of python ex5.py

3 Likes

Hello.
I don’t think you should write:
”python3.6 file.py”.
Just:
”python3 file.py”.

If I run either command on OSX I get the same instance of the interpreter so I don’t think it really matters. In fact, if I create a symlink to python and call it monty I can do monty ex5.py so it’s really down to how your system is configured and how you want it to behave.

Formatted string literals (f-strings) were introduced in Python 3.6 so anything prior will not be able to interpret the code.

I really wish there was a way to make Python barf hard with special instructions when people run the wrong version. I may investigate that, but I doubt it.

I am running into the same issue. I’m on a Windows box with Python 3.7.2 installed as verified by executing ‘python --version’ in PowerShell, but I’m getting a syntax error on line 1 as well. I’ve double checked my quote placement, and I do not have a python3.X command available on my computer.

If I don’t actually have a syntax error, could it be related to running 3.7.X?

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print(f"Lets talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's {my weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")

# This line is tricky, try to get it exactly right
total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight}, I get {total}.")

Output from running ex5.py

[Running] python -u "c:\Users\<username_redacted>\Documents\Projects\python\thw\tempCodeRunnerFile.py"
  File "<fstring>", line 1
    (my weight)
             ^
SyntaxError: invalid syntax

Crikey, neve rmind, I found the issue…missing an underscore in the printf statement for weight. For some reason the error kept referring to line 1 throwing me off.

That always happens. Right after you post looking for an answer you realize the mistake. Try this next time:

Send your post to yourself as an email, then go take a break. It’s magic. You’ll probably see the answer right after you hit send.

2 Likes

If you write print() function in a program and someone using Python 2.x tries to run it, they will get an error. To avoid this, it is a good practice to import print function :

from __future__ import print_function

The from future import print_function ; to bring the print function from Python 3 into Python 2.x. Now your code works on both Python 2.x and Python 3.x . The future statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning.

1 Like

Thank you, this was my solution!!