我正在尝试做这个MOOC
在这堂课上
我写了完全相同的代码,但它对我来说并不一样。我不明白。
我的完整代码在github 上。
class Zone:
ZONE = []
MIN_LONGITUDE_DEGREES = -180
MAX_LONGITUDE_DEGREES = 180
MIN_LATITUDE_DEGREES = -90
MAX_LATITUDE_DEGREES = 90
WIDTH_DEGREES = 1
HEIGHT_DEGREES = 1
def __init__(self, corner1, corner2):
self.corner1 = corner1
self.corner2 = corner2
self.inhabitants = 0
@classmethod # etand donner qu'on ne sommes plus dans l'instance, masi oui dans la classe il faut changer self par cls
def initialize_zones(cls):
for latitude in range(cls.MIN_LATITUDE_DEGREES, cls.MAX_LATITUDE_DEGREES):
for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, WIDTH_DEGREES):
bottom_left_corner = Position(longitude, latitude)
top_right_corner = Position(longitude + cls.WIDTH_DEGREES, latitude + cls.HEIGHT_DEGREES)
zone = Zone(bottom_left_corner, top_right_corner)
cls.ZONE.append(zone)
#zone = Zone(bottem_letf_corner, top_right_corner)
print(len(cls.ZONES))
def main():
for agent_attributes in json.load(open("agents-100k.json")):
latitude = agent_attributes.pop('latitude')
longitude = agent_attributes.pop('longitude')
position = Position(longitude, latitude)
agent = Agent(position, **agent_attributes)
Zone.initialize_zones()
main()
错误是:
回溯(最近一次通话最后):
文件“model.py”,第 64 行,在
main()文件“model.py”,第 62 行,在 main
Zone.initialize_zones()文件“model.py”,第 46 行,在 initialize_zones
for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, WIDTH_DEGREES):NameError:未定义名称“WIDTH_DEGREES”