Login system on Python

3k Views Asked by At

This is a really newbie question. So, I am trying to code a login system in Python, where it asks for an username (only 1 username available), if the username typed in is incorrect, it says the username is invalid, if it is correct, it asks for a password, if the password is incorrect, it says incorrect password and proceeds to ask for the password once again, and if the password typed in is correct, it just says logged in.

What I've been able to do so far is:

a = 0

 while a < 1:             
     print ('Username:')  
     name = input()
     if name != 'Teodor': #checks the username typed in
         print ('Invalid username.') 
         continue
     else:
         print ('Hello, Teodor.')
         print ('Password:')
         a = a + 1

 password = input()

 b = 0
      while b < 1:
     if password != '1234': #checks the password typed in
         print ('Password incorrect.')
         b = b + 1
         continue

     else:
         print ('Password correct.')
         print ('Logging in...')
         print ('Logged in.')
         break

This works, although it does something I don't want if the user types in the incorrect password. If the user types in the incorrect password, I wanted the program to tell the user 'Incorrect password' and proceed to ask for it once again, but it doesn't do that, it just prints 'Incorrect password', and then it terminates. It works 100% on the part where it asks for the username, though.

It's a little thing I am missing. How can I fix this? Thanks a lot!

4

There are 4 best solutions below

0
ILostMySpoon On BEST ANSWER

The statement b = b + 1 is terminating your while loop every time the user enters an incorrect password. There is really no need for it.

You can alternatively just wrap your password prompt in a while loop:

while input("Enter password") != "1234":
    print("Password incorrect")
0
Vaibhav Bajaj On

There is no need for the + 1 when checking for the password. That just breaks you out of the loop.

Instead, try:

if password != '1234': #checks the password typed in
         print ('Password incorrect.')
         continue

A much better solution, instead of using +1 and <1 to break out of loops is to use booleans. Sample:

userCorrect = False
while not userCorrect:
    print ('Username:')
    name = raw_input()
    if name != 'Teodor': #checks the username typed in
        print ('Invalid username.')
        continue
    else:
        print ('Hello, Teodor.')
        print ('Password:')
        userCorrect = True

password = raw_input()

passCorrect = False
while not passCorrect:
    if password != '1234': #checks the password typed in
        print ('Password incorrect.')
        print ('Password:')
        password = raw_input()
    else:
        passCorrect = True
# Since password is correct, continue.
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')
0
EyuelDK On

This loop (while b < 1:) terminates the moment an invalid password is entered.

Look at

>     if password != '1234': #checks the password typed in
>         print ('Password incorrect.')
>         b = b + 1
>         continue

The line of code b = b + 1 makes it such that while b < 1: becomes false, thus ending the loop and terminating your program.

0
Alter On

As others have already pointed out, the problem lies is b = b + 1 breaking the condition while b < 1:, causing it not to ask for another password. Simple delete the line b = b + 1

Want to make it better?

Avoid the 'over-the-shoulder' attack with getpass() instead of input(). Your password input is masked as ****
ex.

from getpass import getpass
password = getpass()

Cryptify
Well, apart from sounding cool, this doesn't really stop somebody from modifying the code to skip past the password phase -but it can stop them from seeing the raw password in the code.

this post has a good example using passlib

This helps protect non-unique/sensitive passwords (like the password you use for 5x other things, or your mother's maiden name... lets not drag her into this)