Page 1 of 1

Occasion Error: name '...' is not defined

Posted: Tue Nov 15, 2016 6:16 pm
by overflow12
Hello,

I am testing a part of my code which converts a binary number to decimal in the bitwise manner. But when I run I got error message " name 'dcmltemp' is not defined " (please refer to the following code for variable name) If I keep run it 2 or 3 times the error goes away and gives correct result. Then, I run it again, the error comes up again.

I cannot see what I did wrong in the code. Please help.

Thanks in advance.

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

clkPin = 23
misoPin = 21
mosiPin = 19
drdy_bPin = 15
	
GPIO.setup(clkPin, GPIO.OUT)
GPIO.setup(misoPin, GPIO.IN)
GPIO.setup(mosiPin, GPIO.OUT)
GPIO.setup(drdy_bPin, GPIO.IN)


if (GPIO.input(drdy_bPin)):   #Data ready is active low
	voltage = 'Waiting for ADC output :)' 
else:
       
        dcmltemp = 0
        dcmlValue = 0
        p = 0
        check = 0

        tempbinValue = 5    # give for test
	tempbinValueNew = 5  #give for test

	for i in range(0, 4):
		check = tempbinValueNew & 1
		if(check == 1):
			dcmltemp = 2**p
		else:
			dcmltemp = 0

		dcmlValue = dcmlValue + dcmltemp
	
		p = p + 1
		tempbinValueNew = tempbinValue
		tempbinValueNew = tempbinValueNew >> p	
				
print('%d' % dcmltemp)
print('%.4f' % dcmlValue)[/color]

GPIO.cleanup()


Re: Occasion Error: name '...' is not defined

Posted: Tue Nov 15, 2016 7:09 pm
by elParaguayo
You only define that variable it your first if test is not satisfied. Either include a line to set tbe variable or change the indents of your print statements

Re: Occasion Error: name '...' is not defined

Posted: Tue Nov 15, 2016 7:50 pm
by overflow12
elParaguayo wrote:You only define that variable it your first if test is not satisfied. Either include a line to set tbe variable or change the indents of your print statements
Hi elParaguayo, I do have variables declared in actual program. I have fixed my post. Thanks.

Re: Occasion Error: name '...' is not defined

Posted: Tue Nov 15, 2016 8:14 pm
by Paeryn
As elParaguayo said, dcmltemp is only defined in the else: part of the initial test. If that isn't run then when it gets to the print at the bottom it won't have been defined.
You have (skipping the bulk of the else: section)

Code: Select all

if (GPIO.input(drdy_bPin)):   #Data ready is active low
   voltage = 'Waiting for ADC output :)' 
else:
        dcmltemp = 0
        ## rest of else clause skipped...
print('%d' % dcmltemp)
If GPIO.input(drdy_bPin) is True then dcmltemp is never defined.
You've got some weird indentation in the else: section too, 8 spaces for the first 5 lines then 3 spaces - that's not valid (indentation within a block has to be consistent).

Re: Occasion Error: name '...' is not defined

Posted: Wed Nov 16, 2016 9:26 am
by elParaguayo
Thanks Paeryn. That explained it more clearly than I managed.

Also, I just noticed the horrible spelling in my first post. Obviously the predictive text on my phone was not up to scratch last night!