-1

I'm trying to do this MOOC

And on this class

I wrote exactly the same code but it doesn't work the same for me. I don't understand.

My full code is on 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()

ERROR was:

Traceback (most recent call last):

File "model.py", line 64, in

main()

File "model.py", line 62, in main

Zone.initialize_zones()

File "model.py", line 46, in initialize_zones

for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, WIDTH_DEGREES):

NameError: name 'WIDTH_DEGREES' is not defined

3 Answers3

2

for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, WIDTH_DEGREES)

should be

for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, cls.WIDTH_DEGREES)


Next time, post your full error traceback

FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
  • now is other insue, i figure out and i will post the full correction, thanks. –  Jul 20 '18 at 11:28
1

So, after all, I had two errors, one of them was the CLS, which I had accidentally tried to insert, but since I had gotten a mistake in it and since I did not pay attention I had two instead of one because in the print it was badly written too.

First of all, as @FHTMichell told and right was:

for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, WIDTH_DEGREES)

should be

for longitude in range(cls.MIN_LONGITUDE_DEGREES, cls.MAX_LONGITUDE_DEGREES, cls.WIDTH_DEGREES)

And on:

@classmethod
def initialize_zones(cls):

the print was wrong too:

print(len(cls.ZONES))

should be

print(len(cls.ZONE))

Thanks @user2653663 too for helping.

0

You have WIDTH_DEGREES instead of cls.WIDTH_DEGREES in your for longitude ... loop.

user2653663
  • 2,818
  • 1
  • 18
  • 22