Login system with infinite loop error
<p>I'm coding myself a "simple" login system everything works ok up until the <code>LOGIN</code> part.</p>

<p>The <code>while</code> loop at the end only happens once even though I put another user rather than what I created when I ran the program. I got it to work at some point, but then there was another issue where the <code>while</code> loop would happen over and over again.</p>

<pre><code>import re
users = {}
status = ""


while status != "r":
    status = input("Press R to register!\n")

    if status == "r":
        createUser = input("Create your Username: ")
        while len(createUser)&lt; 5:                                   #checks user len
            print("Username should contain at least 5 characters!")

            createUser = input("Create your Username: ")            #repeat if user len &lt; 5

        while not re.match("^[a-z]*$" , createUser):                   # denies blank spaces
            print("Cannot use blank spaces")
            createUser = input("Create your Username: ")

        if createUser in users:
            print("Username already used!")
        else:
            createPass = input("Create your Password: ")
            while len(createPass) &lt; 5:                              #checks pass len
                print("Password too short!\n          Password should contain at least 5 characters!\n")
                createPass = input("Create your Password: ")        #repeat if pass len &lt; 5

            while not re.match("^[a-z]*$", createPass):  # denies blank spaces
                print("Cannot use blank spaces")
                createPass = input("Create your Password: ")


            else:
                users[createUser] = createPass  #adds user and pass to users
            print("User created!")

        #LOGIN
            for createUser in users:
                username = input("Username: ")
                if username == createUser in users:
                    password = input("Password: ")
                else:
                    while username != createUser:
                        print("User unregistered! Please register!")
                        createUser = input("Create your Username:")
</code></pre>

<p>Try this:</p>

<pre><code>def login():
    username = input("Username: ")

    if username not in users:
        print("User unregistered! Please register!")
        register()
        return

    password = input("Password: ")

    if users[username] != password
        print("Password invalid")
</code></pre>

<p>I've rewritten your code here. Notice how I've broken it down into functions which do one thing:</p>

<ul>
<li>usernameValidator</li>
<li>passwordValidator</li>
<li>getUsername</li>
<li>getPassword</li>
<li>register</li>
<li>login</li>
</ul>

<p>Beginning of the program:</p>

<pre><code>import re
users = {}
</code></pre>

<p>Now we define some validators to check if the username/password are correct:</p>

<pre><code>def usernameValidator(username):
    errorMessage = ""
    if len(username) &lt; 5:
        errorMessage += "Username should contain at least 5 characters!\n"
    if not re.match("^[a-z]*$" , username):       # Note... this checks for more than just blank spaces!
        errorMessage += "Cannot use blank spaces\n"
    if username in users:
        errorMessage += "Username already used!\n"
    return errorMessage

def passwordValidator(password):
    errorMessage = ""
    if len(password) &lt; 5:
        errorMessage += "Password should contain at least 5 characters!\n"
    if not re.match("^[a-z]*$" , password):       # Note... this checks for more than just blank spaces!
        errorMessage += "Cannot use blank spaces\n"
    return errorMessage
</code></pre>

<p>Now we write the getUsername/getPassword functions which talk with the user:</p>

<pre><code>def getUsername():
    username = input("Create your Username: ")
    errorMsg = usernameValidator(username)
    print(errorMsg)

    return username if errorMsg == "" else ""

def getPassword():
    password = input("Create your Password: ")
    errorMsg = passwordValidator(password)
    print(errorMsg)

    return password if errorMsg == "" else ""
</code></pre>

<p>Putting it all together, we write register/login:</p>

<pre><code>def register():
    username = ""
    password = ""

    while username == "":
        username = getUsername()

    while password == "":
        password = getPassword()

    users[username] = password
    print("User created!")

def login():
    username = input("Username: ")

    if username not in users:
        print("User unregistered! Please register!")
        register()
        return

    password = input("Password: ")

    if users[username] != password:
        print("Password invalid")
</code></pre>

<p>Finally, we may run:</p>

<pre><code>while True:
    status = input("Press R to register!\nPress L to login\n")

    if status.lower() == "r":
        register()
    if status.lower() == "l":
        login()
</code></pre>

<p><a href="https://repl.it/DsrJ" rel="nofollow">Try it online</a>.</p>

python