在代碼最前面加入壹行代碼:import?sys。
出錯是因為沒有導入sys這個模塊。
修改後的代碼變為:
import?sys
import pygame
pygame.init()
screen = pygame.display.set_mode([640,480])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
擴展資料:
Python編程常見問題
問題壹,出現“name ‘name’ is not defined”報錯
解決:"name"兩端是雙下劃線"_",不是只有壹個""。
問題二,出現“name 'messagebox' is not defined”報錯
解決:“ ” 內為某個數據庫的子module。
在代碼中加上語句:from tkinter import messagebox,默認情況下子module不會自動import。
問題三,出現“name 'reload' is not defined.”報錯。
解決:對於 Python 2.X添加代碼行:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
對於 Python 3.3添加代碼行:
import imp
imp.reload(sys)
參考資料: