[SOLVED] Random Walks in python


This Content is from Stack Overflow. Question asked by Nikki

There are classes for Location, Drunk, and Field. I was trying to have a subclass dirty field. That generates a dirty tile in the field. If a random walker moves on the dirty tile then, they keep moving.

I am getting the following error: I am hoping that y’all see something that I don’t see.

AttributeError                            Traceback (most recent call last)
Input In [16], in <cell line: 27>()
     22         self.party[motive] =
     23                 self.party[motive].move(x, y)
     26 start = Location(0, 0)
---> 27 f = dirtyField()
     29 homer = SouthDrunk('Homer')
     30 f.addDrunk(homer, start)

Input In [16], in dirtyField.__init__(self, dirtyTiles, xRange, yRange)
      9 y = random.randint(-yRange, yRange)
     10 aDirtyTile = Location(x, y)
---> 11 self.dirtTiles.append(aDirtyTiles)

AttributeError: 'dirtyField' object has no attribute 'dirtTiles'
class dirtyField(Field):
    def __init__(self, dirtyTiles = 1000,
                 xRange = 100, yRange = 100):
        self.dirtyTiles = []
        w = 0
        while (w < dirtyTiles):
            x = random.randint(-xRange, xRange)
            y = random.randint(-yRange, yRange)
            aDirtyTile = Location(x, y)
    def moveDrunk(self, motive):
        # per instructions if the axis is a dirty tile then the drunk moves until a clean tile.
        # one tile at a time motive is another
        Field.moveDrunk(self, motive)
        while (self.party[motive] in self.dirtTiles):
            self.party[motive] =
                self.party[motive].move(x, y)
            x, y = motive.takeStep()
        self.party[motive] =
                self.party[motive].move(x, y)


I had grammar errors and def moveDrunk was too much. I also needed w+=1 at the init

This Question was asked in StackOverflow by Nikki and Answered by Nikki It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?