- MianQi2019
- Posts: 7
- Joined: Tue Oct 21, 2025 3:14 am
SyntaxError: invalid syntax ---> "
When I typed the example code:
on https://docs.python.org/3/tutorial/controlflow.html in terminal, it always prompt:
What's wrong?
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
Code: Select all
File "<stdin>", line 4
print(f"Found an even number {num}")
^
SyntaxError: invalid syntax
- cleverca22
- Posts: 9593
- Joined: Sat Aug 18, 2012 2:33 pm
Re: SyntaxError: invalid syntax ---> "
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
Re: SyntaxError: invalid syntax ---> "
It looks like the tutorial example changes from to so that might trip up a beginner editing it?
Code: Select all
for n in range(2, 10):Code: Select all
for num in range(2, 10):- MianQi2019
- Posts: 7
- Joined: Tue Oct 21, 2025 3:14 am
Re: SyntaxError: invalid syntax ---> "
Sorry, I made a mistake for "n" and "num", but when I corrected it, the same:
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".
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
>>>
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 ---> "
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
>>>
the edit box isnt using a fixed-width font, but the code block isMianQi2019 wrote: ↑Tue Oct 21, 2025 10:24 amWhat'smore, the "^" targeted to " in terminal, when I pasted it here, it would left shift several spaces, and I moved it right by keyboard.
since you added spaces, the ^ is now pointing to the wrong place
Re: SyntaxError: invalid syntax ---> "
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))
Re: SyntaxError: invalid syntax ---> "
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?
Re: SyntaxError: invalid syntax ---> "
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 !
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 !
Re: SyntaxError: invalid syntax ---> "
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.
Or if OP built Python themself in 2021, then I do not understand how they would not have used 3.9.
Re: SyntaxError: invalid syntax ---> "
A search turned up the same elsewhere ...
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.
- 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 ---> "
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.
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion