简体   繁体   中英

If elif don't work. What's wrong is in my code?

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.

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!")

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.

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()

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)

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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM