What's causing this variable referenced before assignment error?

后端 未结 3 939
深忆病人
深忆病人 2021-01-21 18:58

This is the code that I am working with:

import pygame

global lead_x
global lead_y
global lead_x_change
global lead_y_change

lead_x = 300
lead_y = 300
lead_x_c         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 19:14

    Python doesn't need global variables declared in any special way. Just declare and initialize your variables at the top like you did in the second chunk of code. Then move your global declarations to inside each method that CHANGES your global. So you can cut your first chunk and paste into your method.

    Note that to READ a global you don't need to use the global keyword. This can help keep your constants constant, since Python doesn't handle constants specially.

提交回复
热议问题