1

I am trying to build a locker with Raspberry Pi. I have a code that when entering a correct CODE = '1234' with USB keyboard it opens a servo. Basically it works, but need to loop it somehow so, if I put wrong PIN, I am asked again to put correct.

for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        e = categorize(event)
        if e.keystate == e.key_down:
            klawisz = e.keycode[4:]
            if klawisz != "ESC":
                kod = (kod + klawisz)
                print(kod)
            else:
                break

if kod == '1234':
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            d = categorize(event)
            if d.keystate == d.key_down:
                klawisz = d.keycode[4:]
                if klawisz == "ESC":
                    print('ITS OPEN')
                    break
                else:
                    break
else:
     print('Wrong PIN')

I tried with while loop at the beginning, but it doesn't work :(

while kod == '1234'

Hope you could you guide me to correct solution, as I am still learning Python. Thanks.

1
  • Please edit your question title to make it more meaningful. Also, you should try to abstract your code (your algorithm) further. For example, put the part about reading the password from user input into a function. This will help you create a much shorter MCVE which will result in much clearer questions and you'll get a) better search results when looking for a solution yourself and b) better and faster answers here on SO. Commented Dec 3, 2018 at 22:49

2 Answers 2

1

You can use while loop to repeatedly perform some operations (reading users password in your example) if some condition stays true:

def read_password():
    kod = ""
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            e = categorize(event)
            if e.keystate == e.key_down:
                klawisz = e.keycode[4:]
                if klawisz != "ESC":
                    kod = (kod + klawisz)
                    print(kod)
                else:
                    break
    return kod

while read_password() != '1234':
    print('Wrong PIN, try again')

In this case you're reading password as long as it doesn't match '1234'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is what I was looking for. Works like a charm.
1

Use an infinite while loop and break out of it only if the code matches.

while True:
    code = input('Enter code: ')
    if code == '1234':
        print('Code accepted.')
        break
    print('Wrong code, try again.')

You can easily add an additional security feature to reduce the amount of attempts per time.

import time
attempts = 0
while True:
    code = input('Enter code: ')
    if code == '1234':
        print('Code accepted.')
        break
    print('Wrong code, try again.')
    attempts = attempts + 1
    if attempts > 9:
        print('Too many failed attempts. Please wait.')
        time.sleep(600)
        attempts = 0

You can run all examples above on your regular computer to test them. They use Python's built-in input function. You use the RETURN key to finalize your input instead of the ESC key in your code. You said that you read the user input with a USB keyboard, so input might even work for you with your Raspberry Pi, too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.