Skip to main content

User Creation and Login System in Python

The program below does two things:

1.  It allows a user to login if he or she already has an account on the system.

2.  It allows a user to create an account and login afterwards if the user does not have an account already (New user) in the system.

NOTE:
The program allows the user only two chances in every action. If the user fails again in the second chance, the program denies access from the user.

Try it to find out. It's fun...

You can copy and paste in your development environment and study how the code works  or type it out yourself. Either way, you are good to go.

To login as an existing user use the details below:

Username:  studysourcecodes
Password:  python

The program starts below:



userDict = {"studysourcecodes":"python"}

#Note:  Username = Dictionary 'key'
#            Password = Dictionary 'value'

result = ""
result1 = ""
result2 = ""
result3 = ""
result4 = ""
result5 = ""

print("Welcome!")

def choiceEntry():
global result5
print("\n\nHave an account?  Enter '1' to login.\n\nNew user?  Enter '2' to create an account.")
choice = int(input("\n\nEnter the number here: "))
if choice == 1:
print("\n\nEnter login details:")
username3 = input("Enter user name: ")
password3 = input("Enter password: ")

if username3 in userDict and password3 == userDict[username3]:
print("\n\nWelcome", username3, "\n")
else:
print("Invalid username or password! Try again.")
print("\n\nEnter login details:")
username3 = input("Enter user name: ")
password3 = input("Enter password: ")
if username3 in userDict and password3 == userDict[username3]:
print("Welcome", username3)
else:
print("Invalid username or password! Access denied!")


elif choice == 2:
userDetails()
check()
else:
print("Invalid entry! Try again.")
result5 = 3

def userDetails():
global username
global password1
global password2
firstName = input("\nEnter First Name:")
lastName = input("Enter Last Name: ")
username = input("Enter your username:")
password1 = input("Enter your password: ")
password2 = input("Retype password: ")
email = input("Enter email address: ")

def check():
if password1 == password2:
userDict [username] = password1
print("\nUser successfully created!\nProceed to login.")
print("\n\nNew userDict equals: ",userDict)
login()
else:
global result1
print("\nPasswords do not match!\nTry registering again.")
result1 = 2

def login():
print("\nEnter Login details.")
username2 = input("Enter username: ")
password = input("Enter password: ")

if username2 in userDict and password == userDict[username]:
global result3
#print("\nWelcome ",username)
#print("New userDict equals: ",userDict)
result3 = 0
else:
global result4
print("\nInvalid username or password!\nTry again.\n")
result4 = 1


choiceEntry()
#userDetails()
#check()


if result5 == 3:
print("\nNew user?  Enter '1' to login.\n\nNew user?  Enter '2' to create an account.")
choice = int(input("\n\nEnter the number here: "))
if choice == 1:
#login()
print("\n\nEnter login details:")
username3 = input("\nEnter user name: ")
password3 = input("Enter password: ")
if username3 in userDict and password3 == userDict[username3]:
print("\n\nWelcome", username3, "\n")
else:
print("Invalid username or password! Try again\n\n")
print("\n\nEnter login details:")
username3 = input("\nEnter user name: ")
password3 = input("Enter password: ")
if username3 in userDict and password3 == userDict[username3]:
print("\n\nWelcome", username3, "\n")
else:
print("\n\nInvalid username or password! Access denied!\n")


elif choice == 2:
userDetails()
check()
else:
print("\nInvalid entry! Access denied!")
#result5 = 3

elif result3 == 0:
print("\nWelcome ",username)
elif result4 == 1:
print("\nEnter Login details.")
username2 = input("Enter username: ")
password = input("Enter password: ")

if username2 in userDict and password == userDict[username]:
print("\nWelcome ",username)
else:
#global result4
print("\nInvalid username or password!\nAccess denied!")
result4 = 1


elif result1 == 2:
userDetails()
if password1 == password2:
userDict [username] = password1
print("\nUser successfully created!\nProceed to login.")
print("\n\nNew userDict equals: ",userDict)
login()

if result4 == 1:
#print("Try again.")
print("\nEnter Login details.")
username2 = input("Enter username: ")
password = input("Enter password: ")

if username2 in userDict and password == userDict[username]:
print("\nWelcome ",username)
else:
#global result4
print("\nInvalid username or password!\nAccess denied!")

else:
#global result1
print("\nPasswords do not match!\nYou have entered many unmatched passwords! \nTry again in 2 minutes time.")

else:
pass

The program ended above!

Have any questions or contribution to make? Let us know in the comment box below.

UPCOMING TUTORIAL:
User Creation and Login system in python (Graphic User Interface)

Source codes will also be available here.

Comments