Ros basic obstacle detection not working

The program should follow this pattern. But I don’t know where my code is going wrong as whenever my robot takes a left turn it gets hit by a wall and doesn’t move further. Please help!!
I HAVE ADDED MY CODE IN THE COMMENT SECTION!

  1. If the laser reading in front of the robot is higher than 1 meter (there is no obstacle closer than 1 meter in front of the robot), the robot will move forward.
  2. If the laser reading in front of the robot is lower than 1 meter (there is an obstacle closer than 1 meter in front of the robot), the robot will turn left.
  3. If the laser reading at the right side of the robot is lower than 1 meter (there is an obstacle closer than 1 meter at the right side of the robot), the robot will turn left.
  4. If the laser reading at the left side of the robot is lower than 1 meter (there is an obstacle closer than 1 meter at the left side of the robot), the robot will turn right.

MY CODE
#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

rospy.init_node(‘turtlebot3_node_one’)

def callback(msg):

if msg.ranges[360] > 1 :

    var.linear.x = 0.5

    pub.publish(var) 

if msg.ranges[360] < 1  :

    var.angular.z = 0.5

    var.linear.x = 0.5

    pub.publish(var) 

if msg.ranges[0] < 1 :

    var.angular.z =  0.5

    var.linear.x = 0.5

    pub.publish(var) 

if msg.ranges[719] < 1 :

   var.angular.z = -0.2

    var.linear.x = 0.2

    pub.publish(var)

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

rate = rospy.Rate(1)

var = Twist()

#var.linear.z = 0.5

#var.angular.x = 0.5

while not rospy.is_shutdown():

#pub.publish(var)

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

rate.sleep()