SyntaxError Occurs in the Program as Written in the Book

Asked 2 years ago, Updated 2 years ago, 391 views

I've read it over and over again, but I can't help but get an error even though they're all the same programs.

import pygame aspg,sys
pg.init()
screen=pg.display.set_mode(800,600))

while True:
    screen.fill(pg.Color("WHITE"))
    pg.draw.rect(screen, pg.Color("RED"), (100, 100, 150))
  pg.display.update()
    for event inputg.event.get():
        if event.type==pg.QUIT:
            pg.quit()
            sys.exit()

The pg part of pg.display.update() turns red and says SyntaxError.
I am using an IDLE for Python 3.6

python pygame python-idle

2022-09-30 22:03

2 Answers

Write down all the error messages in the question without abbreviating them.
Or, it may look like a question on the editor screen, but if I try it, won't it appear like this?(However, the path name is the file name in the environment I tried.)

File "C:\Develop\Python\pg.py", line 8
      pg.display.update()
    ^
SyntaxError: invalid non-printable character U+3000

If the source code is still copied instead of being mixed up in the question article, the indentation of the line is not 4 half-width blank but 2 full-width blank.
Correct it to 4 half-width spaces and it will work.

"By the way, there is ""Check Module"" on the ""Run"" menu on the Python-IDLE editor screen, which you can check if you run it, but in Python 3.8 the pg part is red (or pink?) and the message box is displayed under the ""Syntax Error"" title."

If this is Python 3.9 or 3.10, the full-width blank at the beginning of the line will be pink, and the sentence part will be changed to invalid non-printable character U+3000 as shown above.

Perhaps the number of Python versions used in the reference book is 3.6, so I think I combined the number of Python versions used, but if the minor version (the number immediately after 3.) is different, both should be installed, so you should consider including the latest version (3.10).

Additional Note:
After checking the IDLE above, I suddenly thought about it and tried Python 3.8 and found that the error message changed as shown in IDLE.This is the actual situation of the question.
I understand that the check content varies depending on the number of Python versions.The new version of the message is closer to the cause of the problem.

File "C:\Develop\Python\pg.py", line 8
      pg.display.update()
       ^
SyntaxError: invalid character identifier


2022-09-30 22:03

Visual Studio Code is recommended for editors.
It's convenient to use full-width spaces and find grammatical errors.

Enter a description of the image here


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.