0

我正在尝试使用 python 和 sqlite 制作密码管理器。我将变量设置为 0 并在每次用户输入错误密码时将其递增 1,一旦变量x等于 3,程序应关闭,但变量x仅递增 1 次,然后保持为 1。我该如何解决这个问题?

master = 'pass'


def main():
    inp = input("\nPassword\n>>> ")
    global x
    x = 0
    if inp == master:
        print("Welcome Back!\n")
        print("1. View Database\n2. Edit Database\n")
        task = input(">>> ")
        if task == 0:
            pass
        else:
            pass
    else:
        if x == 3:
            print("You have failed to enter the correct password 3 times")
            os.exit()
        else:
            x += 1
            print(x)
            main()
main()
4

1 回答 1

0

我对此的解决方案是放置x = 0外部def main():部分。如果你这样做,增量应该对所有代码都有好处。

x = 0
def main():
    global x
    [rest of code]

否则,您每次在main()循环内设置 x = 0。

于 2020-11-01T21:44:53.767 回答