Pygame window not appearing

I am experiencing an issue when running the following code on my Mac, using Pygame:

import pygame, sys
from pygame.locals import*

pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 800
RED = (255,0,0)
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
screen.fill(RED)




pygame.display.update()

The code runs without any error messages, but the display window doesn’t appear.

The issue is that the Pygame window is not being updated and displayed on the screen. To fix this, move the pygame.display.update() line inside the main game loop, like this:

import pygame, sys
from pygame.locals import*

pygame.init()
SCREENWIDTH = 800
SCREENHEIGHT = 800
RED = (255,0,0)
screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))

pygame.draw.rect(screen, RED, (400, 400, 20, 20),0)
screen.fill(RED)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()

This will continuously update the Pygame window until the user closes it.