Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SyntaxError: multiple statements found while compiling a single statement

1,799 views
Skip to first unread message

Cai Gengyang

unread,
Oct 8, 2016, 2:02:17 AM10/8/16
to
Any idea how to correct this error ? Looks fine to me ....

>>> rect_x = 50

# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop

SyntaxError: multiple statements found while compiling a single statement

Thanks ...

Steve D'Aprano

unread,
Oct 8, 2016, 2:19:26 AM10/8/16
to
On Sat, 8 Oct 2016 05:02 pm, Cai Gengyang wrote:

> Any idea how to correct this error ? Looks fine to me ....

As usual, the first and most important rule of debugging is to use ALL the
information the compiler gives you. Where is the rest of the traceback? At
the very least, even if nothing else, the compiler will print its best
guess at the failed line with a caret ^ pointing at the place it realised
there was an error.

Asking us to debug your problem without giving us ALL the information is
like that old joke:


A man makes an appointment at the doctor because he isn't feeling
well. He goes in to see the doctor, who says "So, Mr Smith, what
seems to be the problem?"

The man says "You're the medical expert, you tell me."


I do not think it is possible to get the error message you claim from
running the code you show. I think there is more code that you haven't
shown us, and that the error is there.

The only way I can reproduce the error message you give is by using the
compile() function:

py> compile("a=1\nb=2", "", "single")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
a=1
^
SyntaxError: multiple statements found while compiling a single statement


Notice how the compiler shows the offending line and puts a caret ^ at the
end?

Unless you show us what code actually causes this error message, I don't
think it is possible to help you. We're not mind-readers.





--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

Cai Gengyang

unread,
Oct 8, 2016, 2:32:26 AM10/8/16
to
Unfortunately, in this case, it is 100% of the information I am giving you. You can try it yourself. The code is in the first section (8.1) of

http://programarcadegames.com/index.php?chapter=introduction_to_animation&lang=en#section_8

Just copy and paste it into your Python IDLE and let me know what you get ...

Steve D'Aprano

unread,
Oct 8, 2016, 3:02:09 AM10/8/16
to
On Sat, 8 Oct 2016 05:29 pm, Cai Gengyang wrote:

> Unfortunately, in this case, it is 100% of the information I am giving
> you.

Exactly. That's the problem. You need to give us more information, like how
you are trying to run this code. Are you using an IDE? Which IDE?


> You can try it yourself. The code is in the first section (8.1) of
>
>
http://programarcadegames.com/index.php?chapter=introduction_to_animation&lang=en#section_8
>
> Just copy and paste it into your Python IDLE and let me know what you get


I don't normally use IDLE, and you shouldn't assume that everyone does.
*That* is the extra information we need to solve the problem:

The IDLE interactive interpreter in Python 3 does not seem to allow you to
paste multiple lines at once. If you do, it highlights the first line of
code with a red background and prints the error:

SyntaxError: multiple statements found while compiling a single statement



For example:


Python 3.3.0rc3 (default, Sep 27 2012, 18:44:58)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
Type "copyright", "credits" or "license()" for more information.
>>> # Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
SyntaxError: multiple statements found while compiling a single statement
>>>



I can't show the colour highlighting, but the line BLACK = ... is
highlighted in red.


You can either copy and paste one line at a time, or you can use the File >
Open menu command to open a complete script.

Cai Gengyang

unread,
Oct 8, 2016, 3:51:50 AM10/8/16
to
This is the result when I copy and paste it one line at a time :

>>> rect_x = 50
>>> rect_y = 50
>>> while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
SyntaxError: invalid syntax

Rustom Mody

unread,
Oct 8, 2016, 7:07:47 AM10/8/16
to
On Saturday, October 8, 2016 at 1:21:50 PM UTC+5:30, Cai Gengyang wrote:
> This is the result when I copy and paste it one line at a time :
>
> >>> rect_x = 50
> >>> rect_y = 50
> >>> while not done:
> for event in ...
<etc>

There's sure something strange about your formatting

you want something like this I think:

rect_x = 50
rect_y = 50
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

On a related note in addition to Steven's 2 suggestions
- paste 1 line at a time
- Write the code in a py file

you could also try it out without Idle
ie start python at your shell (or terminal or cmd.exe or whatever)
And try it there

bream...@gmail.com

unread,
Oct 8, 2016, 7:46:57 AM10/8/16
to
The easiest way is to use the ipython %paste magic command.

Kindest regards.

Mark Lawrence.

Cai Gengyang

unread,
Oct 8, 2016, 10:48:40 AM10/8/16
to
Somehow it still doesnt work --- it keeps giving the syntaxerror, inconsistent use of tabs and indentation message EVEN though i use only the enter and space buttons and never touched the tab button a single time. Im pretty sure there is something wrong with the program itself. Gonna download pycharm and start testing the programs in it --- heard that it gives a better user experience with indentations

Terry Reedy

unread,
Oct 8, 2016, 5:59:09 PM10/8/16
to
On 10/8/2016 2:56 AM, Steve D'Aprano wrote:

>> Just copy and paste it into your Python IDLE and let me know what you get
>
>
> I don't normally use IDLE, and you shouldn't assume that everyone does.
> *That* is the extra information we need to solve the problem:
>
> The IDLE interactive interpreter in Python 3 does not seem to allow you to
> paste multiple lines at once.

Actually, it does. I pasted the following multiline statement.

>>> while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
while not done:
NameError: name 'done' is not defined

ie, the single statement was compiled.

>>>> # Define some colors
> BLACK = (0, 0, 0)
> WHITE = (255, 255, 255)

> SyntaxError: multiple statements found while compiling a single statement

Here you pasted multiple *statements* (on separate lines). This is a
known difference between console and IDLE Shell. It is partly related
to being line versus statement oriented, partly due to window paste
being handled by system console versus tk => tkinter => IDLE run by
python. Note that in a console, each line is preceded by the primary
prompt ('>>> '). Ditto if one types the lines into IDLE.

There has been some discussion of changing IDLE, but it would not be
trivial. The console splits pasted text into lines and feeds a line at
a time to Python. IDLE would have to intercept the pasted text before
tk displays it, parse (split) the paste into statements (rather than
lines), and display and run one statement at a time. I would rather
spend the effort on making it possible to run code in a new editor
window without explicitly saving it.

--
Terry Jan Reedy

Gregory Ewing

unread,
Oct 8, 2016, 8:15:07 PM10/8/16
to
Cai Gengyang wrote:
> Somehow it still doesnt work --- it keeps giving the syntaxerror,
> inconsistent use of tabs and indentation message EVEN though i use only the
> enter and space buttons and never touched the tab button a single time.

There was another thread about this a short time ago.
It turns out that when you press enter in the IDLE REPL,
it auto-indents the next line -- but it does it using
*tabs*, not spaces. So if you do your manual indentation
with spaces, or paste something in that uses spaces, you
can easily end up with invalid tab/space mixtures.

The solution is to always use the tab key for indentation
when working interactively in IDLE.

I think this should be reported as a bug in IDLE, because
it's very unintuitive behaviour, and a beginner has little
chance of figuring out what's going on by themselves.

--
Greg

Cai Gengyang

unread,
Oct 8, 2016, 10:52:04 PM10/8/16
to
This is my latest result : I copy and pasted one line at a time into the IDLE and used ONLY the "enter-return" button to move on to the next line and this time I didnt get an indentation error but instead a traceback error

>>> rect_x = 50
>>> rect_y = 50
>>> while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True


Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
while not done:
NameError: name 'done' is not defined

















Steve D'Aprano

unread,
Oct 8, 2016, 11:42:33 PM10/8/16
to
On Sun, 9 Oct 2016 01:51 pm, Cai Gengyang wrote:

> This is my latest result : I copy and pasted one line at a time into the
> IDLE and used ONLY the "enter-return" button to move on to the next line
> and this time I didnt get an indentation error but instead a traceback
> error:

> Traceback (most recent call last):
> File "<pyshell#12>", line 1, in <module>
> while not done:
> NameError: name 'done' is not defined

Right.

That's because 'done' is not defined.

Why don't you try my suggestion of saving the code into a .py file, then
using the File > Open command to open it?

Cai Gengyang

unread,
Oct 8, 2016, 11:52:25 PM10/8/16
to
I defined both done and pygame in this piece of code, but now i get a new error that i have never seen before, an AttributeError


>>> rect_x = 50
>>> rect_y = 50
>>> done = False
>>> pygame = True
>>> while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True


Traceback (most recent call last):
File "<pyshell#48>", line 2, in <module>
for event in pygame.event.get():
AttributeError: 'bool' object has no attribute 'event'

Terry Reedy

unread,
Oct 9, 2016, 1:37:15 AM10/9/16
to
On 10/8/2016 11:51 PM, Cai Gengyang wrote:

>>>> pygame = True
>>>> while not done:
> for event in pygame.event.get():

> Traceback (most recent call last):
> File "<pyshell#48>", line 2, in <module>
> for event in pygame.event.get():
> AttributeError: 'bool' object has no attribute 'event'

pygame == True and True has not attribute 'event'. Believe the error
messages until you have real reason to question them. They are perhaps
99.9% accurate by now/

--
Terry Jan Reedy

Steve D'Aprano

unread,
Oct 9, 2016, 2:42:06 AM10/9/16
to
On Sun, 9 Oct 2016 02:51 pm, Cai Gengyang wrote:

> I defined both done and pygame in this piece of code, but now i get a new
> error that i have never seen before, an AttributeError

AttributeError usually means you have the wrong kind of object:

py> mylist = {} # oops, a dict not a list
py> mylist.append(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'append'


or sometimes you have the right object but misspelled the attribute or
method:

py> mylist = []
py> mylist.apend(1) # oops, spelling error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'apend'
py> mylist.append(1)
py> print(mylist)
[1]



>>>> rect_x = 50
>>>> rect_y = 50
>>>> done = False
>>>> pygame = True

Why have you defined pygame = True? Is that what the code on the website
does?

My guess is that you are supposed to say:

import pygame

Rustom Mody

unread,
Oct 9, 2016, 2:51:45 AM10/9/16
to
One meta-suggestion:
It may be that with python you can go further in less time than with
older languages like C++, Java etc.

But less time doesn’t mean no time — You still need to learn, to walk the
learning curve

In short: Dont skimp on the tutorial: https://docs.python.org/3/tutorial/
even (especially) if you find the examples uninteresting/irrelevant

khushi...@gmail.com

unread,
Sep 24, 2017, 7:14:00 AM9/24/17
to
0 new messages