Python Forum
Name 'function' not defined, although it is...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Name 'function' not defined, although it is...
#1
Hey,

I want to write some program for a 4x4 button matrix, where it simply outputs the number of the button I press.

I'm using this code:

import time
import RPi.GPIO as GPIO

GPIO.setwarnings(True)

class keypad():
	def __init__(self, columnCount = 4):
		GPIO.setmode(GPIO.BCM)
		#Button settings
		if columnCount is 4:
			self.KEYPAD = [
				[1,2,3,4],
				[5,6,7,8],
				[9,10,11,12],
				[13,14,15,16]
			]
		#RPI PIN-Allocation
			self.ROW = [18,23,24,25]
			self.COLUMN = [4,17,22,21]
		else:
			return

	def getKey(self):
		#Setting all columns as output
		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.OUT)
			GPIO.output(self.COLUMN[j], GPIO.LOW)
		#Setting all rows as input
		for i in range(len(self.ROW)):
			GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
		#Scanning rows for pressed buttons
		rowVal = -1
		for i in range(len(self.ROW)):
			tmpRead = GPIO.input(self.ROW[i])
			if tmpRead == 0:
				rowVal = i

		if rowVal < 0 or rowVal > 3:
			self.exit()
			return

		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

		GPIO.setup(self.ROW[rowVal], GPIO.OUT)
		GPIO.output(self.ROW[rowVal], GPIO.HIGH)

		colVal = -1
		for j in range(len(self.COLUMN)):
			tmpRead = GPIO.input(self.COLUMN[j])
			if tmpRead == 1:
				colVal = j

		if colVal < 0 or colVal > 3:
			self.exit()
			return

		#Returning the pressed button
		self.exit()
		return self.KEYPAD[rowVal][colVal]

	def exit(self):
		#Re-initializing all rows and columns
		for i in range(len(self.ROW)):
			GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)
		for j in range(len(self.COLUMN)):
			GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down = GPIO.PUD_UP)

	if __name__ == '__main__':
		#Initializing the keypad
		kp = keypad()
		#Endless loop to check for pressed buttons
		while True:
			digit = None
			while digit == None:
				digit = kp.getKey()
			#Return of the button pressed
			print digit
			time.sleep(0.5)
But when running it, I get the error:
Error:
Traceback (most recent call last): File "matrix.py", line 6, in <module> class keypad(): File "matrix.py", line 71, in keypad kp = keypad() NameError: name 'keypad' is not defined
Apparently the main function doesn't know that keypad() doesn't exists...

I've searched in other threads for solutions, but haven't found any that were fitting to my problem.
From these I only learned that you need to define functions, before you use them, but here that's clearly the case in my eyes. In fact, it's pretty much the first thing I did.

So I am a little baffled...

Any help is appreciated! Big Grin
Reply
#2
Indentation of lines 69 and next is wrong. They should not be part if the class. Dedent by one level
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-23-2020, 12:38 PM)buran Wrote: Indentation of lines 69 and next is wrong. They should not be part if the class. Dedent by one level

now it seems so obvious...

Thanks tho! Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,162 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,773 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,236 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,589 May-25-2022, 07:22 PM
Last Post: Gribouillis
Sad Function defined by branches antoniogalante 1 1,821 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  function call at defined system time? Holon 5 3,154 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Function will not return variable that I think is defined Oldman45 6 3,435 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 2,207 Jul-24-2020, 01:39 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020