import pygame
running = True
while running:
for event in pygame.event.get_pressed():
if event.type == pygame.QUTI:
running = False
if not(running):
pygame.quit()
break #you got the error because you didn't completely close pygame so add
#so add break after pygame.quit()