bool和if语句出现问题

时间:2019-02-15 11:45:55

标签: python

我正在构建一个ID确认系统,用户有3次尝试键入正确的ID,否则,系统将退出。我的问题是,当用户在第三次输入最后一次输入正确的ID时,当代码正确时,语句写出ID正确,而第二行显示了他,表明他输入了过多的尝试和系统正在退出。我该如何解决错误? 我该如何解决,当用户在3/3尝试中键入正确的ID时,系统会说,更正,我们可以破坏if语句。 我的代码:

checkas = True
Attempts = 3
CurrentAttemp = 0
KickForBadCode = sys.exit
while checkas:
    CurrentAttemp += 1
    IDConfirm = input("* [3NEMATIX]: {} Please confirm Your ID CODE... Attempt! {}/{} ".format(Vartotojo_Vardas, CurrentAttemp, Attempts))
    with open (DuomenuBaz, mode = 'r', encoding = 'utf-8') as Confirm:
        for line in Confirm:
            if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():
                print("Correct!")
                Confirm.close()
                checkas = False
                break
            elif "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip() and CurrentAttemp > 2:
                clear()
                print("Correct!")
                Confirm.close()
                checkas = False
                break
            elif CurrentAttemp >2:
                clear()
                print("~ You have been kicked for too many attempts!")
                checkas = False
                sys.exit

2 个答案:

答案 0 :(得分:1)

仅当exitCurrentAttemp>时,才需要Attempts。以及Vartotojo_Pass<=Attempts是正确的。

尝试替换此部分:

if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():
                print("Correct!")
                Confirm.close()
                checkas = False
                break
            elif "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip() and CurrentAttemp > 2:
                clear()
                print("Correct!")
                Confirm.close()
                checkas = False
                break
            elif CurrentAttemp >2:
                clear()
                print("~ You have been kicked for too many attempts!")
                checkas = False
                sys.exit

与此:

        if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():
            print("Correct!")
            Confirm.close()
            checkas = False
            break
        elif "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip() and CurrentAttemp < Attempts:
            clear()
            print("Correct!")
            Confirm.close()
            checkas = False
            break
        elif CurrentAttemp > Attempts:
            clear()
            print("~ You have been kicked for too many attempts!")
            checkas = False
            sys.exit

答案 1 :(得分:0)

我修复了它,它包含过多的if语句,这导致脚本每次都重新启动,并且脚本将无法运行。问题是只有if语句。

 checkas = True
    Attempts = 3
    CurrentAttemp = 0
    KickForBadCode = sys.exit
    while checkas:
        CurrentAttemp += 1
        if CurrentAttemp > 3:
            checkas = False
            clear()
            print("~ You have been kicked for too many attempts!")
            sys.exit
        else:
            IDConfirm = input("* [3NEMATIX]: {} Please confirm Your ID CODE... Attempt! {}/{} ".format(Vartotojo_Vardas, CurrentAttemp, Attempts))
            with open (DuomenuBaz, mode = 'r', encoding = 'utf-8') as Confirm:
                for line in Confirm:
                    if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():
                        print("Correct!")
                        Confirm.close()
                        checkas = False
                        break
                    else:
                        checkas = True
                        clear()