Learn to Code via Tutorials on Repl.it!

← Back to all posts
# A basic account management Python Program - Signing Up and Logging In
multicoder2021 (4)

YOU HAVE TO TYPE THIS CODE

You have to type this code:

import os
def clear():
  os.system('clear')

Making a login/signup feature

There are two ways to do this. One of which aren't really secure.

I'll do it from the least secure to the most secure.

Least Secure Option

So first, we have to type x = 1 then on the next line while x == 1:.
[We have to indent everything in this loop.]
Then, we have to ask the user for input, whether they have an account or not.
Just use the input function.

accountornot = input("""
Do you have an account[y\\n]?
>>> 
""")
clear() #You use this function that you defined earlier. It clears all the output for a fresh console. This is by far the most useful feature I have found out in Python.

So far so good.

Then we have to use an if else loop.

The Signing Up part

So first, we type if accountornot == "n":.
Then...
The signing up part.
First, we need a TEXT file.
You can do this by copying and pasting in this code:

f = open("names.txt", "r")
f.close()

or if you know how to create a text file, then do name it "names.txt"
Then, ask the user for a new Username:

NEWchathandle = input("What is your New Username?\n\n\t\t>>>\t")
clear()

and store it in the text file...

f = open("names.txt", "w+")
f.write("\n" + NEWchathandle)
f.close()
x = 0 #This breaks out of the loop

And that's all for the sign up process.
I told you it wasn't that secure.

The logging in part.

This is EZ!
First, you obviously type else:
Then, you ask for the username:

username = input("What is your Username?\n\n\t\t>>>\t")
clear()

Then you check if it is in the text file:

f = open("names.txt", "r")
if(username in f.read()):
  print("Username Found!")
  x = 0
  clear()
else:
  pass #This makes sure the loop continues
f.close()

The whole code

And I have very modestly typed this code for you to copy and paste :)

x = 1
while x == 1:
  accountornot = input("""
  Do you have an account[y\\n]?
  >>> 
  """)
  if accountornot == "n":
    NEWchathandle = input("What is your New Username?\n\n\t\t>>>\t")
    clear()
    f = open("names.txt", "w+")
    f.write("\n" + NEWchathandle)
    f.close() 
    x = 0 #This breaks out of the loop
  else:
    username = input("What is your Username?\n\n\t\t>>>\t")
    clear()
    f = open("names.txt", "r")
    if(username in f.read()):
      print("Username Found!")
      x = 0
      clear()
   else:
      pass #This makes sure the loop continues
f.close()

The most secure way

This is basically the same, but that instead of just storing a USERNAME in a TEXT FILE, we will store a USERNAME and PASSWORD in a DATABASE.
So we are actually using the replit database, no SQL databases.
First of ALL, you type from replit import db
Second of ALL, you then type keys = db.keys(). db.keys() are basically the keys in the database.
After That, do the x = 1 and while x == 1:
Then, let's get the info if the has a account or not, its the same as above.
Proceeding this, we do the if accountornot == "n":
Then we get the USERNAME and PASSWORD.
Then, type this code:
db[NEWchathandle] = NEWpassword
This stores a key with a name of the new username and its value is new password.
Finally: type x = 0
Then you do the else:
Then you get one input, "username".
Then you type: realpassword = db[username]
Here, I'll show the rest:

if(username in keys):
  password = input("What's your password?")
  if password == realpassword:
    print("Authorisation Process Complete.")
    x = 0
  else:
     print("INVALID PASSWORD!!!!!!!!!!!!!")
     clear()
else:
  print("INVALID USERNAME!!!!!!!!!!!!!!!!!!")

That's pretty much it.

OF course... the whole code...

from replit import db
keys = db.keys()
x = 1
while x == 1:
  accountornot = input("""
  Do you have an account[y\\n]?
  >>> 
  """)
  if accountornot == "n":
    NEWusername = input("What is your NEW username?\n\n\t\t>>>\t")
    NEWpassword = input("What is your NEW password?\n\n\t\t>>>\t") 
    global username
    username = NEWusername
    db[NEWusername] = [NEWpassword]
    x = 0
  else:
    username = input("What is your username?\n\n\t\t>>>\t")
    if(username in keys):
      global username
      USERNAME = username
      username = USERNAME
      password = input("What's your password?")
      if password == realpassword:
        print("Authorisation Process Complete.")
        x = 0
     else:
       print("INVALID PASSWORD!!!!!!!!!!!!!")
       clear()
    else:
      print("INVALID USERNAME!!!!!!!!!!!!!!!!!!")
      password = input("What is your password?\n\n\t\t>>>\t")

The End

Thanks for seeing this tutorial!

If this gets at least 4 votes, I will post a second part. I know it's a lot, but I believe we can achieve it.

Cya Later!

THE ACTUAL END

Comments
hotnewtop
ch1ck3n (2388)

do you know what a UI is

multicoder2021 (4)

Actually, that was something I was planning to do for a future tutorial. I have renamed it now.

multicoder2021 (4)

@ch1ck3n And yes, I do know what a UI is

codingjlu (484)

ok so where's the UI?

DynamicSquid (5027)

I would recommend testing the code first to make sure it actual works before writing a tutorial on it, since there's some major errors/bugs with it...