Hellfire
Hellfire

Reputation: 95

How to create a pin checker

I'm very new to python and trying to create a pin checker. Iv'e done most of it, but Iv'e got a problem with trying to get the code to reject the users attempts to enter letters or a combination of letters and numbers, so the code treats it as a incorrect answer. My code just crashes if letters are entered. I tried removing the int before the input statement, which makes it reject letters like I want it to, but now longer accepts the correct code.


pin = {1111}  # Correct pin code

count = 1        # Count function to allow code to recongise restrict pin attempts to three and to terminate code if three incorrect pins are entered

while True:
    enter_pin = int(input("Enter your pin: ")); # Ask user for pin code
    if (enter_pin) in pin:
        print("")
        print("Correct pin")
        print("")
        print("Access granted")   # Grant access if correct pin entered
        break
    print("")
    print("Incorrect pin. Please try again")   # Repeat question and allow another attempt if wrong pin is given
    print()
    count += 1
    if count == 4:
        print("Incorrect pin entered three times, system locked")
        break

What I want:

Enter your pin: 46166

Incorrect pin. Please try again

Enter your pin: egsrgsd

Incorrect pin. Please try again

Enter your pin: 1111

Correct pin

Access granted

Upvotes: 0

Views: 2239

Answers (3)

What about something like this:

pin = {1111}  # Correct pin code

count = 1  # Count function to allow code to recongise restrict pin attempts to three and to terminate code if three incorrect pins are entered

while True:
    try:
        enter_pin = int(input("Enter your pin: "))  # Ask user for pin code
        if not enter_pin in pin:
             raise ValueError("Incorrect PIN")
        print("")
        print("Correct pin")
        print("")
        print("Access granted")  # Grant access if correct pin entered
        break
    except ValueError as e:
        print("")
        print("Incorrect pin. Please try again")  # Repeat question and allow another attempt if wrong pin is given
        print()
        count += 1
        if count == 4:
            print("Incorrect pin entered three times, system locked")
            break

Here you can catch the errors of entering the wrong pin, or entering letters.

Upvotes: 0

The Laggy Tablet
The Laggy Tablet

Reputation: 100

Try using the try and except for this problem. What it means is that, it will execute the code in the try block first and if it encounters a problem, will go to the except block and execute the code in it.

In this case, we transfer this part of the code to the try block

        enter_pin = int(input("Enter your pin: ")) # Ask user for pin code
        if (enter_pin) in pin:
            print("")
            print("Correct pin")
            print("")
            print("Access granted")   # Grant access if correct pin entered
            break

And transfer the part of code to the except block when the input is wrong.

    print("Incorrect pin. Please try again")
    print()
    count += 1
    if count == 4:
        print("Incorrect pin entered three times, system locked")
        break

So the complete code would be this -

pin = {1111}  # Correct pin code

count = 1        # Count function to allow code to recognise restrict pin attempts to three and to terminate code if three incorrect pins are entered

while True:
    enter_pin = int(input("Enter your pin: ")) # Ask user for pin code
    try:
        if (enter_pin) in pin:
            print("")
            print("Correct pin")
            print("")
            print("Access granted")   # Grant access if correct pin entered
            break
        print("")
    except ValueError:
        print("Incorrect pin. Please try again")   # Repeat question and allow another attempt if wrong pin is given
        print()
        count += 1
        if count == 4:
            print("Incorrect pin entered three times, system locked")
            break

Upvotes: 0

alani
alani

Reputation: 13079

You can use exception handling to write a function for entering the pin, where if there is an invalid input then it will try again (loops indefinitely until the return ... statement succeeded). The ValueError is the exception which is raised when trying to convert non-numeric string into an int (which currently will cause your program to abort, when the try ... except is not used).

def get_pin():
    while True:
        try:
            return int(input("Enter your pin: "))
        except ValueError:
            print("you must enter a number")

Then when you are currently doing

    enter_pin = int(input("Enter your pin: ")); # Ask user for pin code

change it to call the function:

    enter_pin = get_pin()

Upvotes: 1

Related Questions