overflow12
Posts: 2
Joined: Tue Nov 15, 2016 5:54 pm

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

Tue Nov 15, 2016 6:16 pm

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()

Last edited by overflow12 on Tue Nov 15, 2016 7:47 pm, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Tue Nov 15, 2016 7:09 pm

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
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

overflow12
Posts: 2
Joined: Tue Nov 15, 2016 5:54 pm

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

Tue Nov 15, 2016 7:50 pm

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.

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

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

Tue Nov 15, 2016 8:14 pm

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).
She who travels light — forgot something.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Wed Nov 16, 2016 9:26 am

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!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”