We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

11 posts • Page 1 of 1
MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

SyntaxError: invalid syntax ---> "

Tue Oct 21, 2025 3:37 am

When I typed the example code:

Code: Select all

for n in range(2, 10):
 for x in range(2, n):
 if n % x == 0:
 print(f"{n} equals {x} * {n//x}")
 break
4 equals 2 * 2
6 equals 2 * 3
8 equals 2 * 4
9 equals 3 * 3
on https://docs.python.org/3/tutorial/controlflow.html in terminal, it always prompt:

Code: Select all

 File "<stdin>", line 4
 print(f"Found an even number {num}")
 ^
SyntaxError: invalid syntax
What's wrong?

cleverca22
Posts: 9593
Joined: Sat Aug 18, 2012 2:33 pm

Re: SyntaxError: invalid syntax ---> "

Tue Oct 21, 2025 6:58 am

MianQi2019 wrote:
Tue Oct 21, 2025 3:37 am
What's wrong?
the code you supplied doesnt match the error message, so there is no way to know the answer

{num} appears nowhere in the code you claim to have ran

neilgl
Posts: 11309
Joined: Sun Jan 26, 2014 8:36 pm

Re: SyntaxError: invalid syntax ---> "

Tue Oct 21, 2025 8:30 am

It looks like the tutorial example changes from

Code: Select all

for n in range(2, 10):
to

Code: Select all

for num in range(2, 10):
so that might trip up a beginner editing it?

MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

Re: SyntaxError: invalid syntax ---> "

Tue Oct 21, 2025 10:24 am

Sorry, I made a mistake for "n" and "num", but when I corrected it, the same:

Code: Select all

python3
Python 3.5.3 (default, Nov 4 2021, 15:29:10) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for n in range(2,10):
... for x in range(2, n):
... if n % x == 0:
... print(f"{n} equals {x} * {n//x}")
 File "<stdin>", line 4
 print(f"{n} equals {x} * {n//x}")
 ^
SyntaxError: invalid syntax
>>> 
What'smore, the "^" targeted to " in terminal, when I pasted it here, it would left shift several spaces, and I moved it right by keyboard.

There is other clues: when I typed at first, the " would be output as @(2) and typed @(2) would show ", in this situation, either I input @ or " would get the same prompt. I resolved this by setting in "Keyboard Layout..." as "Generic 105 key - English(US)", after this when I typed shift+', it would show ", while it still prompt "SyntaxError: invalid syntax".

cleverca22
Posts: 9593
Joined: Sat Aug 18, 2012 2:33 pm

Re: SyntaxError: invalid syntax ---> "

Tue Oct 21, 2025 10:58 am

Code: Select all

$ python3
Python 3.13.7 (main, Aug 14 2025, 11:12:11) [GCC 14.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for n in range(2,10):
... for x in range(2, n):
... if n % x == 0:
... print(f"{n} equals {x} * {n//x}")
... 
4 equals 2 * 2
6 equals 2 * 3
6 equals 3 * 2
8 equals 2 * 4
8 equals 4 * 2
9 equals 3 * 3
>>> 
works fine when i run that same code
MianQi2019 wrote:
Tue Oct 21, 2025 10:24 am
What'smore, the "^" targeted to " in terminal, when I pasted it here, it would left shift several spaces, and I moved it right by keyboard.
the edit box isnt using a fixed-width font, but the code block is
since you added spaces, the ^ is now pointing to the wrong place

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: SyntaxError: invalid syntax ---> "

Thu Oct 23, 2025 1:15 pm

MianQi2019 wrote:
Tue Oct 21, 2025 10:24 am
Python 3.5.3 (default, Nov 4 2021, 15:29:10)
That's likely your problem right there. Python f-strings didn't exist until Python 3.6

The error marker '^' is probably way off to the right because of invisible trailing spaces in the source code copied.

Try replacing the print with -

Code: Select all

print("{} equals {} * {}".format(n, x, n // x))

jojopi
Posts: 4360
Joined: Tue Oct 11, 2011 8:38 pm

Re: SyntaxError: invalid syntax ---> "

Thu Oct 23, 2025 1:34 pm

hippy wrote:
Thu Oct 23, 2025 1:15 pm
That's likely your problem right there. Python f-strings didn't exist until Python 3.6
Very well spotted, hippy.

Python 3.6 was released in December 2016, and by 2019 Debian buster came with Python 3.7.

How does OP have Python 3.5.3, built in 2021 but using a GCC dated 2017?

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: SyntaxError: invalid syntax ---> "

Thu Oct 23, 2025 2:24 pm

Debian Stretch had Python 3.5 as the default. I would guess that had bug fixes and maybe some features back-ported, re-compiled in 2021 with GCC 6.3 which was the default for Stretch, then distributed via 'apt'.

My guess is the OP has a 3B+ or earlier, maybe a Zero W, installed Stretch which was the latest at the time, never upgraded the OS but had kept updated with 'apt'.

It's easy to forget how old some Pi products are; 3B was introduced in 2016, 3A+/B+ in 2018, 4B in 2019.

I am using a 3B to set-up a Trixie SD Card to be moved to my 4B. The 4B doesn't feel 6 years old !

jojopi
Posts: 4360
Joined: Tue Oct 11, 2011 8:38 pm

Re: SyntaxError: invalid syntax ---> "

Thu Oct 23, 2025 3:08 pm

Stretch would explain the Python 3.5.3 and the GCC 6.3.0. But it was end-of-life before 2021. I think its last python3 update was 2017.

Or if OP built Python themself in 2021, then I do not understand how they would not have used 3.9.

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: SyntaxError: invalid syntax ---> "

Thu Oct 23, 2025 5:38 pm

A search turned up the same elsewhere ...

CAPTURE.jpg
CAPTURE.jpg (11.97 KiB) Viewed 789 times

https://microdigisoft.com/controlling-raspberry-pi-gpio-using-telegram-app

And there's an even older 2021 version shown in an earlier image. So I presume, end of life or not, there were updates being delivered up until late 2021 at least.

Unless that's also the OP I can't believe they both built their own version at exactly the same time.

MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

Re: SyntaxError: invalid syntax ---> "

Tue Nov 04, 2025 8:54 am

hippy wrote:
Thu Oct 23, 2025 2:24 pm

My guess is the OP has a 3B+ or earlier, maybe a Zero W, installed Stretch which was the latest at the time, never upgraded the OS but had kept updated with 'apt'.
Right, I tried those code on a 3B+ bought more than 9 years ago, when I tried them on Ubuntu 24.04 just now, it was all OK. Thanks.

11 posts • Page 1 of 1

Return to "Python"

AltStyle によって変換されたページ (->オリジナル) /