繁体   English   中英

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

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

我试图通过 PIN 码进行安全设置,但它不起作用……怎么办?

我一直在尝试创建程序,该程序要求输入 PIN。 如果你写错 3 次 PIN 码,程序会和你说再见,然后程序结束。 此外,如果您编写正确的 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

如果我启动程序,它会告诉我“写下你的 PIN 码”。 如果我第一次输入正确的 PIN 码,程序将继续下一步,但如果我输入 3 次错误的 PIN 码,程序也将继续。 我一直试图以其他方式修复它......我试图通过while循环修复它,除了for循环,但它不起作用。 我不知道现在出了什么问题......我会感谢每一个反应和建议。

这将做同样的事情

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

您的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

除非您绝对需要,否则我还建议使用break而不是quit()

删除第一个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()

此代码将执行相同的操作,以更清洁的方式。

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)

我认为您必须将条件中的尝试次数与输入一起包括在内。 你可以尝试这样的事情:

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