-1

I'm trying to recreate a plot that uses matplotlib by using plotly and whenever I call my function I catch an error saying,

Traceback (most recent call last):
  File "c:\Users\gramb\python_projects\project_2\rw_plotly\random_walk_p.py", line 38, in <module>
    rw.fill_walk()
  File "c:\Users\gramb\python_projects\project_2\rw_plotly\random_walk_p.py", line 20, in fill_walk
    x_step, y_step = _get_step(), _get_step()
NameError: name '_get_step' is not defined

while the function is written as such in its class:

    def __init__(self, num_points=5000):
        """Initialize attributes of a walk."""
        self.num_points = num_points

        # Walks start at (0, 0).
        self.x_values = [0]
        self.y_values = [0]
    
    def fill_walk(self):
        """Calculates all points of the walk."""

        # Take steps until max length
        while len(self.x_values) < self.num_points:
            # Decide direction and distance    
            x_step, y_step = _get_step(), _get_step()

            # Start loop from top if both are 0
            if x_step == 0 and y_step == 0:
                continue
            
            # Calculate new position
            x, y = self.x_values[-1] + x_step, self.y_values[-1] + y_step

            self.x_values.append(x), self.y_values.append(y)

    def _get_step(self):
        """Creates a step on the walk."""
        direction = choice([1, -1])
        distance = choice(range(5))
        return direction * distance

rw = RandomWalk()
rw.fill_walk()

The error did not occur until after I had installed pandas with pip, but I had also uninstalled it to make sure that was not the problem yet the issue still occurs.

I'm using VS Code with a few extensions for python, so if that info is needed, please let me know.

2 Answers2

1

You need to use self._get_step instead of just get_step. This will run the function under the instance of the class.

Top Of Tech
  • 305
  • 2
  • 11
1

you defined the _get_step method inside class So to call current instance method inside class you have to use self._get_step()