Sts-pi explorer phat line follower

Hi all,
I’m new to the pi but have worked few a few little projects, My big aim for getting into raspberry pi is to be able to provide digital maker sessions within my scout group so please be patient its for a good cause :)
I’m trying to write the programme (as the title describes) to build a line following robot using a raspberry pi 3b+, sts-pi kit, explorer phat, and a pair of line sensors.
i have previously driven the buggy around with a programme and also made the sensors work and print out results when the input changes, however I cannot for the life of me manage to get my programme to continue reading the sensors and play the loop continuously to make it an autonomous line follower, i have tried copying other peoples programmes and altering them for the explorerhat library and also tried writing a whole new one.
my latest code is here, could someone point out what i am doing wrong please. thanks in advance.
import explorerhat

buggyf = explorerhat.motor.forwards
buggyl = explorerhat.motor.one.forwards
buggyr = explorerhat.motor.two.forwards

while True:
if (left.is 1 == True) and (right.is 1 == True):
left = explorerhat.input.one.read()
right = explorerhat.input.two.read()
buggyf(40)
elif (left.is 1 == False) and (right.is 1 == True):
left = explorerhat.input.one.read()
right = explorerhat.input.two.read()
buggyl(40) and buggyr(0)
elif (left.is 1 == True) and (right.is 1 == False):
left = explorerhat.input.one.read()
right = explorerhat.input.two.read()
buggyr(40) and buggyl(0)
else:
buggyf(0)

What happens when you run it?
Code tags here are three ` before your code and three after. Do that and it will retain the indents etc.

while True:
    etc

when i run the program the buggy will go forwards and just keep going, i cant even stop it by stopping the program, i have to load up a second program and run that.

shouldn’t the
left = explorerhat.input.one.read()
right = explorerhat.input.two.read()
be before the
if (left.is 1 == True) and (right.is 1 == True):
???

This is what I used with my Explorer pHat, using a wireless keyboard for control.

#!/usr/bin/env python
import os
import usb.core
import usb.util
import explorerhat
import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)  

x=(2) #shutdown variable
S=(100) #speed default value.
M=(0) # 0 = Not moving, 1 = Moving Forward, 2 = Moving Backward,



USB_VENDOR  = 0x1997 # Rii
USB_PRODUCT = 0x2433 # Mini Wireless Keyboard

USB_IF      = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS

BTN_LEFT  = 80 # <
BTN_RIGHT = 79 # >
BTN_DOWN  = 81 # V
BTN_UP    = 82 # ^
BTN_STOP  = 44 # Space
BTN_EXIT  = 41 # ESC
BTN_OK    = 40 # OK
BTN_ONE   = 30 # 1
BTN_TWO   = 31 # 2
BTN_THREE = 32 # 3
BTN_FOUR  = 33 # 4
BTN_FIVE  = 34 # 5
BTN_SIX   = 35 # 6
BTN_SEVEN = 36 # 7
BTN_EIGHT = 37 # 8
BTN_NINE  = 38 # 9
BTN_ZERO  = 39 # 0

dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]

if dev.is_kernel_driver_active(USB_IF) is True:
  dev.detach_kernel_driver(USB_IF)

usb.util.claim_interface(dev, USB_IF)

def Shutdown(channel):  
    global x
    x = (0)


GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

while True:
    control = None
    try:
        control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
        print(control)
    except:
        pass

    if control != None:
        if BTN_ONE in control:
          S = (10)

        if BTN_TWO in control:
          S = (20)

        if BTN_THREE in control:
          S = (30)

        if BTN_FOUR in control:
          S = (40)

        if BTN_FIVE in control:
          S = (50)
            
        if BTN_SIX in control:
          S = (60)

        if BTN_SEVEN in control:
          S = (70)

        if BTN_EIGHT in control:
          S = (10)

        if BTN_NINE in control:
          S = (90) 

        if BTN_ZERO in control:
          S = (100)

        if BTN_DOWN in control and M != 1: # Go into reverse if not moving forward, and stop turning if turning.
          explorerhat.motor.backwards(100)
          M = 2
            
        if BTN_UP in control and M != 2: # Go into forward if not moving in reverse, and stop turning if turning.
          explorerhat.motor.forwards(100)
          M = 1
            
        if BTN_LEFT in control and M == 0: # Turn left while stoped
          explorerhat.motor.two.forwards(50)
          explorerhat.motor.one.backwards(50)
            
        if BTN_RIGHT in control and M == 0: # Turn right while stoped
          explorerhat.motor.two.backwards(50)
          explorerhat.motor.one.forwards(50)
            
        if BTN_LEFT in control and M == 1: # Turn left while moving forwards
          explorerhat.motor.two.forwards(100)
          explorerhat.motor.one.forwards(50)
            
        if BTN_RIGHT in control and M == 1: # Turn right while moving forwards
          explorerhat.motor.two.forwards(50)
          explorerhat.motor.one.forwards(100)
            
        if BTN_LEFT in control and M == 2: # Turn left while moving backwards
          explorerhat.motor.two.backwards(100)
          explorerhat.motor.one.backwards(50)
            
        if BTN_RIGHT in control and M == 2: # Turn right while moving backwards
          explorerhat.motor.two.backwards(50)
          explorerhat.motor.one.backwards(100)
            
        if BTN_OK in control: # Stop moving and stop turning 
          explorerhat.motor.stop()
          M = 0
            
        if BTN_EXIT in control:          
          raise SystemExit

        if BTN_STOP in control:
          os.system("sudo shutdown now -P")

    time.sleep(0.02)

    if x == 0:
        os.system("sudo shutdown now -P")
    elif x == 1:
        raise SystemExit

thank you i will have another crack

Hi all,
here is the final working program, i hope it may help others :)
(indents are not correct)

import explorerhat
from time import sleep

buggyf = explorerhat.motor.forwards
buggyl = explorerhat.motor.one.forwards
buggyr = explorerhat.motor.two.forwards

def changed(input):
state = input.read()
name = input.name
print(“Input {} changed to {}”.format(name,state))
explorerhat.input.changed(changed)

left = explorerhat.input.one.read()
right = explorerhat.input.two.read()

while True:

left = explorerhat.input.one.read()
right = explorerhat.input.two.read()

if (left == 1) and (right == 1):
buggyf(10)

elif (left == 0) and (right == 1):
buggyl(10)
buggyr(0)

elif (left == 1) and (right == 0):
buggyr(10)
buggyl(0)

else:
explorerhat.motor.stop()