0

I tried to make the security by PIN code, but it's not working... how to do it?

I've been trying to create program, which ask for PIN. If you write the PIN 3 times incorrect, the program is going to say you goodbye and then program ends. Also if you write correct PIN CODE, the program allow you to continue to the next step of the code.

import time

money=1000

pokus=0
pin=7863

print("Vitajte v programe bankomat!")
for i in range(3):
    vstup=int(input("Zadaj Váš 4-číselný PIN: "))
    if vstup == 7863:
        break
    elif vstup != 7863:
        pokus=pokus+1
        print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))
        continue
        if pokus == 3:
            print("3x zadaný zlý PIN kód. DOVIDENIA!")
            time.sleep(3)
            quit()
        elif pokus < 3:
            continue

If I start the program, it says me "Write your PIN CODE." If I write the PIN CODE correct on the first try, program will continue to next step, but if I write 3 times incorrect PIN code, the program is going to continue too. I've been tried to fix it somehow else... I've tried to fix it by while loop except of for loop, but it doesn't work. I don't know what is wrong now... I'll be thankful for every reactions and advices.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
Author Climent
  • 83
  • 1
  • 1
  • 8
  • You want to end your code after three incorrect attempts or continue it? – Hrithik Puri Sep 27 '19 at 10:22
  • the Continue after print("Nesprávny PIN kód.....) SKIP the code If below – Wonka Sep 27 '19 at 10:22
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Sep 27 '19 at 10:44

5 Answers5

3

This will do the same thing

cnf = 0
print("Vitajte v programe bankomat!")
for i in range(3):
    vstup=int(input("Zadaj Váš 4-číselný PIN: "))
    if vstup == 7863:
        cnf = 1
        break
    elif vstup != 7863:
        print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-i))
        continue
if cnf !=1:
    print("3x zadaný zlý PIN kód. DOVIDENIA!")
Hrithik Puri
  • 286
  • 1
  • 3
  • 20
1

Your placement of continue is faulty. Remove the continue:

import time

money=1000

pokus=0
pin=7863

print("Vitajte v programe bankomat!")
for i in range(3):
    vstup=int(input("Zadaj Váš 4-číselný PIN: "))
    if vstup == 7863:
        break
    elif vstup != 7863:
        pokus=pokus+1
        print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))
        # removed the continue here
        if pokus == 3:
            print("3x zadaný zlý PIN kód. DOVIDENIA!")
            time.sleep(3)
            quit()
        elif pokus < 3:
            continue

I would also suggest break instead of quit() unless you absolutely require it.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
1

Remove the first continue and the last elif (as it is useless):

import time

money=1000

pokus=0
pin=7863

print("Vitajte v programe bankomat!")
for i in range(3):
    vstup=int(input("Zadaj Váš 4-číselný PIN: "))
    if vstup == 7863:
        break
    elif vstup != 7863:
        pokus=pokus+1
        print("Nesprávny PIN kód. Počet zostávajúcich pokusov: {}".format(3-pokus))

        if pokus == 3:
            print("3x zadaný zlý PIN kód. DOVIDENIA!")
            time.sleep(3)
            quit()
Joan Lara
  • 1,362
  • 8
  • 15
1

This code will do the same, be in a more cleaner way.

import time

correct_pin_code = 7863
incorrect_inputs = 0

while(incorrect_inputs <= 3):
    if(incorrect_inputs == 3):
        print("Too many failed attemps, quitting")
        quit()
    input_pin = int(input("Enter your 4 digits PIN code"))
    if(input_pin == correct_pin_code):
        print("Correct PIN code")
        break
    else:
        print("Wrong PIN code, " + str(3 - incorrect_inputs) + "attemps left")
        time.sleep(3)
bastantoine
  • 582
  • 4
  • 21
0

I think you have to include the number of attemps in the condition along with the input. You could try something like this:

pin = 7863
test = 2

while int(input()) != pin and test > 0:
  print("Incorrect pin. Try again.")
  test -= 1

if test == 0:
  print("quit")
  quit()

print("OK. Continue the next phase.")
Fred Lass
  • 1
  • 1