无法退出登录系统

时间:2018-10-15 11:20:00

标签: python loops login

我不会中断登录系统。我已经向我的CS老师展示了此内容,他告诉我更改休息时间的缩进方式。没用我一直在环顾四周,但似乎找不到导致循环中断的原因。这是代码:

while True:
    print("User 1")
    login=input("Do you have an account? (yes/no) ")
    loggedin=False
    if login.lower()=="yes":
        login1=open("login.csv")
        reader = csv.reader(login1)
        username1=input("What is your username: ")
        password1=input("What is your password: ")
        for row in reader:
            if row[0]==username1:
                if row[1]==password1:
                    print("Welcome " + username1)
                    loggedin=True
                    break
        if loggedin==False:
            print("Invalid username or password. Please try again.")
            continue

代码询问用户是否有一个帐户。当我输入是并提供有效的用户名和密码时,它将继续循环。对于用户登录时的上下文,它会显示“欢迎”并退出循环。但是,这没有发生,而是说“欢迎”并重新启动循环。此csv包含登录详细信息。

我们非常感谢您提供有关循环不会中断的帮助。

迪伦

2 个答案:

答案 0 :(得分:3)

像这样?我的意思是您已经有一个“ loggedin”变量,为什么不在while循环中使用它...

loggedin = False
while not loggedin:
    print("User 1")
    login=input("Do you have an account? (yes/no) ")
    loggedin=False
    if login.lower()=="yes":
        login1=open("login.csv")
        reader = csv.reader(login1)
        username1=input("What is your username: ")
        password1=input("What is your password: ")
        for row in reader:
            if row[0]==username1 and row[1]==password1:
                print("Welcome " + username1)
                loggedin=True
                break
        if loggedin==False:
            print("Invalid username or password. Please try again.")
            continue

答案 1 :(得分:0)

您缺少break

您会退出for循环,但不会进入while循环。

尝试一下:

while True:
    print("User 1")
    login=input("Do you have an account? (yes/no) ")
    loggedin=False
    if login.lower()=="yes":
        login1=open("login.csv")
        reader = csv.reader(login1)
        username1=input("What is your username: ")
        password1=input("What is your password: ")
        for row in reader:
            if row[0]==username1:
                if row[1]==password1:
                    print("Welcome " + username1)
                    loggedin=True
                    break
        if loggedin==False:
            print("Invalid username or password. Please try again.")
            continue
        if loggedin==True:
            break

编辑:

就我个人而言,我会避免使用while True,而是这样做:

loggedin = False
while not loggedin:
    print("User 1") # Consider moving this outside of the while loop too
    login=input("Do you have an account? (yes/no) ")
    if login.lower() == "yes":
        login1=open("login.csv") # Consider moving this outside of the while loop too
        reader = csv.reader(login1) # Consider moving this outside of the while loop too
        username1=input("What is your username: ")
        password1=input("What is your password: ")
        for row in reader:
            if row[0]==username1 and row[1]==password1:
                print("Welcome " + username1)
                loggedin = True
                break
        if loggedin==False:
            print("Invalid username or password. Please try again.")

请注意,这里不需要continue