Topics quiz, error with the code please help

so i am brushing up on my ros basics and decided to write the code for topics quiz using oop, but the problem is i keep getting the following error.

.
my code is as follows.

#! /usr/bin/env python3

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

class MoveTurtle():

def __init__(self):

    

    self.sub = rospy.Subscriber('/kobuki/laser/scan',LaserScan, self.callback)

    self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)

    self.vels = Twist()

    self.rate = rospy.Rate(2)

    self.lidar_data = LaserScan()

    

def callback(self, msg):

    rospy.loginfo('callback is called')

    self.lidar_data = msg

            

def turtle_mover(self):  

    while not rospy.is_shutdown():

        rospy.wait_for_message('/kobuki/laser/scan', LaserScan, timeout=None)

        print(self.lidar_data[359])

        if self.lidar_data[359] > 1:

            self.vels.linear.x = 0.2

            self.vels.angular.z = 0.0

            self.pub.publish(self.vels)

            rospy.loginfo('exited the loop')

    

        if self.lidar_data[359] < 1:

            self.vels.linear.x = 0.2

            self.vels.angular.z = 0.5  # left direction

            self.pub.publish(self.vels)

    

        if self.lidar_data[0] < 1:

            self.vels.linear.x = 0.2

            self.vels.angular.z = 0.5

            self.pub.publish(self.vels)

    

        if self.lidar_data[719] < 1:

            self.vels.linear.x = 0.5

            self.vels.angular.z = -0.5  # right direction

            self.pub.publish(self.vels)

        self.rate.sleep()

if name == ‘main’:

rospy.init_node('topics_quiz_node')

mt = MoveTurtle()

rospy.loginfo('publishing into /cmd_vel topic')

mt.turtle_mover()

I think the problem is that you are trying to print the ray number 359 with print(self.lidar_data[359]) but you are not accessing properly to the ranges of the LaserScan message.

In order to access the ranges, you need to indicate as follows:

print (self.lidar_data.ranges[359])

The LaserScan message is composed of several fields, one of those is the ranges field. That is the one that contains the array of laser measurements

ah yes, i forgot to make that change, thank you for pointing it out, pretty easy to overlook smallest changes in the code. thank youu.