简体   繁体   English

如果 elif 不起作用。 我的代码有什么问题?

[英]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?我试图通过 PIN 码进行安全设置,但它不起作用……怎么办?

I've been trying to create program, which ask for PIN.我一直在尝试创建程序,该程序要求输入 PIN。 If you write the PIN 3 times incorrect, the program is going to say you goodbye and then program ends.如果你写错 3 次 PIN 码,程序会和你说再见,然后程序结束。 Also if you write correct PIN CODE, the program allow you to continue to the next step of the code.此外,如果您编写正确的 PIN 代码,该程序允许您继续下一步的代码。

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."如果我启动程序,它会告诉我“写下你的 PIN 码”。 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.如果我第一次输入正确的 PIN 码,程序将继续下一步,但如果我输入 3 次错误的 PIN 码,程序也将继续。 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.我一直试图以其他方式修复它......我试图通过while循环修复它,除了for循环,但它不起作用。 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.您的continue位置错误。 Remove the continue :删除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.除非您绝对需要,否则我还建议使用break而不是quit()

Remove the first continue and the last elif (as it is useless):删除第一个continue和最后一个elif (因为它没用):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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