bool和if语句的问题

2024-09-30 04:37:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个ID确认系统,用户有3次尝试键入正确的ID,如果没有,系统将退出。我的问题是,当用户在最后一次输入正确的ID时,第三次输入正确的ID,当代码正确时,语句写的ID是正确的,第二行显示他,他输入了太多的尝试,系统正在退出。如何修复错误? 我该如何修复,当用户在3/3的尝试中键入正确的ID时,系统会说,correct we can break the 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

Tags: 用户inidfalse键入系统lineconfirm
2条回答

只有当CurrentAttemp大于>时,才需要exit。如果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

我修复了它,它有太多的if语句,导致脚本每次都重新bool,脚本无法正常工作。问题只是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()

相关问题 更多 >

    热门问题