bool 和 if 语句的问题

我正在构建一个 ID Confirm 系统,用户有 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


忽然笑
浏览 159回答 2
2回答

月关宝盒

您exit只需要在CurrentAttempis >than时才需要Attempts。而如果Vartotojo_Pass是在正确的<=到Attempts。尝试更换这部分:if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Confirm.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip() and CurrentAttemp > 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Confirm.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif CurrentAttemp >2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("~ You have been kicked for too many attempts!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit有了这个:&nbsp; &nbsp; &nbsp; &nbsp; if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Confirm.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip() and CurrentAttemp < Attempts:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Confirm.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; elif CurrentAttemp > Attempts:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("~ You have been kicked for too many attempts!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit

慕尼黑5688855

我修复了它,它有太多的 if 语句,导致脚本每次都重新布尔值,并且脚本无法运行。问题只是 if 语句。&nbsp;checkas = True&nbsp; &nbsp; Attempts = 3&nbsp; &nbsp; CurrentAttemp = 0&nbsp; &nbsp; KickForBadCode = sys.exit&nbsp; &nbsp; while checkas:&nbsp; &nbsp; &nbsp; &nbsp; CurrentAttemp += 1&nbsp; &nbsp; &nbsp; &nbsp; if CurrentAttemp > 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("~ You have been kicked for too many attempts!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IDConfirm = input("* [3NEMATIX]: {} Please confirm Your ID CODE... Attempt! {}/{} ".format(Vartotojo_Vardas, CurrentAttemp, Attempts))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with open (DuomenuBaz, mode = 'r', encoding = 'utf-8') as Confirm:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for line in Confirm:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if "Vardas: "+Vartotojo_Vardas + " Password: " + Vartotojo_Pass + " ID: " + IDConfirm in line.strip():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Correct!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Confirm.close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkas = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clear()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python