Hey there, I am stuck on some code I am working on. I am pretty new to coding and this is my first project with Python so i'm sure this chunk of code has much to be desired. Basically I am looking to create a locker system where one can enter a password on a keypad (im using a 4x3 keypad from adafruit along with a raspberry pi 3B+ and a pad4pi library)and actuate a lock solenoid. Additionally I want the user to be able to set their own password of their choosing after pressing a specific button (in this case, *). So far I have been able to manage using the keypad and a default password to actuate the lock but I am running into problems when trying to implement the changing password part. Mainly not being able to change it more than once and the pressed key no longer printing after the first one is pressed(but the key press is still recognized) Any advice/help would be greatly appreciated!

from pad4pi import rpi_gpio import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(20, GPIO.OUT) KEYPAD = [['1','2','3'], ['4','5','6'], ['7','8','9'], ['*','0','#']] ROW_PINS = [17, 25, 24, 22] COL_PINS = [27, 4, 23, 18] factory = rpi_gpio.KeypadFactory() keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS) def defaultPassword(): password='1234' return defaultPassword def changePassword(): newPassword=input('enter new password') return newPassword def testNewPassword(key): for j in range(3): GPIO.setup(COL_PINS[j], GPIO.OUT) GPIO.output(COL_PINS[j], 1) for i in range(4): GPIO.setup(ROW_PINS[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) password=changePassword() attempt = "" try: while (True): for j in range(3): GPIO.output(COL_PINS[j], 0) for i in range(4): if GPIO.input(ROW_PINS[i]) == 0: time.sleep(0.01) while (GPIO.input(ROW_PINS[i]) == 0): pass attempt += KEYPAD[i][j] if len(attempt) == len(password): if attempt == password: #open lock print ("Password Correct") GPIO.setup(21, GPIO.OUT) GPIO.output(20, GPIO.HIGH) else: #keep lock closed print ("Password Incorrect") GPIO.setup(21, GPIO.IN) GPIO.output(20, GPIO.LOW) attempt="" GPIO.output(COL_PINS[j], 1) except KeyboardInterrupt: GPIO.cleanup() def testDefaultPassword(key): for j in range(3): GPIO.setup(COL_PINS[j], GPIO.OUT) GPIO.output(COL_PINS[j], 1) for i in range(4): GPIO.setup(ROW_PINS[i], GPIO.IN, pull_up_down=GPIO.PUD_UP) password=defaultPassword() attempt = "" try: while (True): for j in range(3): GPIO.output(COL_PINS[j], 0) for i in range(4): if GPIO.input(ROW_PINS[i]) == 0: time.sleep(0.01) while (GPIO.input(ROW_PINS[i]) == 0): pass attempt += KEYPAD[i][j] if len(attempt) == len(password): if attempt == password: #open lock print ("Password Correct") GPIO.setup(21, GPIO.OUT) GPIO.output(20, GPIO.HIGH) else: #keep lock closed print ("Password Incorrect") GPIO.setup(21, GPIO.IN) GPIO.output(20, GPIO.LOW) attempt="" GPIO.output(COL_PINS[j], 1) except KeyboardInterrupt: GPIO.cleanup() def printKey(key): print(key) if (key=='*'): changePassword() testNewPassword(key) else: testDefaultPassword() #printKey will be called each time a button is pressed keypad.registerKeyPressHandler(printKey)