为什么我的程序登录循环?(蟒蛇)



>您好,我正在尝试让登录系统使用文本文件工作,它从那里读取密码,但我得到一个无限循环?

我的代码:

inFile = open('passwords.txt', 'r')
print("Welcome To Kellys")
print("This is the admin panel.")
print("You will now be asked for An Admin password.")
print("_____________________________________________________________________________")
print("Would You like to Login(1), Signup(2)")
logindecis = input("Decision: ")
if logindecis == '1':
    ask2 = ("What is Your password?: ")
elif logindecis == '2':
    asknewpass = input("Create A password now: ")
    f = open("passwords.txt","w")
    f.write(asknewpass)
    f.write("n")
    f.close()
else:
    print("Wrong")

# Login bit
login_check = False
while login_check != True:
    if ask2 in inFile:
        print("Logged in")
        login_check = True
    else:
        ask2 = input("What is Your Password: ")

menu_on=True
while menu_on == True:
    create_stock_table()
    create_time_table()
    create_user_table()
    print("------------------------------------------")
    print("|  To Look at the Menu press 1.          |")
    print("|  To calculate pay press 2.             |")
    print("|  To Look at the opening hours press 3. |")
    print("|  To add new customer details press 4.  |")
    print("|  To log out press 5.                   |")
    print("|  To configure DB press 10.             |")
    print("|  To Email us Press 7.                  |")
    print("------------------------------------------")

我的显示器:

Would You like to Login(1), Signup(2)
Decision: 2
Create A password now: leighton
What is Your Password: leighton
What is Your Password: leighton
What is Your Password:

或者如果我按 1:

Would You like to Login(1), Signup(2)
Decision: 1
What is Your Password: leighton
What is Your Password: leighton
What is Your Password: leighton

我实际上没有看到这里有什么问题,所以我需要第二双眼睛来帮助解决这个问题

你可能想做

inFile = open('passwords.txt', 'r')
content = inFile.read()
login_check = False
while login_check != True:
    if ask2 in content and asknewpass in content:
        # ...

原始循环:

login_check = False
while login_check != True:
    if ask2 in inFile and asknewpass in inFile:
        print("Logged in")
        login_check = True
    else:
        ask2 = input("What is Your Password: ")

您的代码循环直到login_check True,唯一的方法是如果ask2asknewpass处于inFile。 但是asknewpass只有在您选择选项 2 时才会被设置。 如果你选择选项1asknewpass in inFile将永远False,所以它将永远循环。 删除检查asknewpass in inFile,如下所示:

    if ask2 in inFile:
        print("Logged in")
        login_check = True

最新更新