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

时间:2017-02-06 15:40:48

标签: python

你好我想让一个登录系统使用文本文件工作,从那里读取密码,但我得到一个无限循环?

我的代码:

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

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

2 个答案:

答案 0 :(得分:0)

你可能想做

{{1}}

答案 1 :(得分:0)

原始循环:

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_checkTrue,并且唯一的方法是ask2asknewpass位于inFile。但是,如果您选择选项asknewpass,则只会设置2。如果您选择1选项,asknewpass in inFile将始终为False,因此它将永远循环播放。删除对asknewpass in inFile的检查,如下所示:

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