Function Variable Question

I have the following function. I am getting an undefined variable “x” error. How can I tell Python this variable is an unsigned int variable? This variable is only used in this function.

def SmackAbort_Function(channel):
    global Abort_Flag;
        
#Debut Point
    print("In Smack Abort Function");
    
    if Abort_Flag:
 
 #Debug Point
        print("Abort Flag already set to true returning")
        
        return
    else:

#Debug Point
         #Debug Point
        print("Seting Abort Flag to true");
        
        Abort_Flag = True;
        ClickBuzzer()
        GPIO.remove_event_detect(Abort_IN_Pin)
        GPIO.output( Abort_Out_Pin, True) # Tell logic controler to abort current task
        
#Wait to make sure the button is release
        while x <= 25:
            if not (GPIO.input(Abort_In_Pin)):
                x = +1;
            else:
                x = 0;
            sleep(0.050);
	
	
# Renable push button interrupt so we do not have an interrupt while running the function code
        GPIO.add_event_detect(Abort_IN_Pin ,
                          GPIO.RISING ,
                          callback = Push_Button_Function,
                          bouncetime = 100) 
        
#Debut Point
    print("Existimg Smack ABORT Function");

The short answer is that you can’t.

In Python, you don’t declare a variable, you just assign to it.

If you haven’t assigned to it, it doesn’t exist.

So, just assign an initial value.

If U meant x =+ 1 x += 1, U can replace it with x = (x + 1) & (2**8 - 1). Where 8 is a placeholder for the bit-width U need.

I think you meant x += 1.

I also think that it should be initialising x to 0 before the loop, and x is limited to the range 0…26, so it’s not necessary to do any masking to limit it to the range of an unsigned 8-bit int.

2 Likes

Thank you for sharing your wisdom with me :smiley: :smiley: :smiley: