I'm about 1-2 weeks into learning Python. I'm learning by making random projects and learning from Youtube/StackOverflow/etc. as I progress. I spent a chunk of my day doing a rudimentary login system that allows you to create an account and simply returns True if you successfully log in one of two ways.
Keep in mind I'm a beginner to programming, so any advice on redundancy/bad habits/#commentyourdamncode/etc. is especially helpful. Thanks!!
PLEASE suggest good practice projects! I will do my best and post them when they're ready.
main.py
sql.py
Keep in mind I'm a beginner to programming, so any advice on redundancy/bad habits/#commentyourdamncode/etc. is especially helpful. Thanks!!
PLEASE suggest good practice projects! I will do my best and post them when they're ready.
main.py
Code:
import os
import sys
import sql
from time import sleep #stay tuned for login attempt limit DLC only $9.99
from sql import add, printDB, searchDB
loggedOn = False
def newAccount():
newUser = input("Please enter your desired username.")
newPassword = input("Please enter your desired password.")
auth = valid(newPassword) #condense this&next line to "if valid(newPassword)"
if auth == True:
print("Accepted! You are now logged on as", newUser, ".")
add(newUser, newPassword)
loggedOn = True
else:
print("Password choice not accepted.")
newAccount()
def valid(password__): #this can be expanded later
if len(password__) < 6:
return False
else:
return True
def login():
user = input("Please enter your username. ")
password = input("Please enter your password. ")
if searchDB(user, password):
print("Congratulations", user, "you have been logged on!")
loggedOn = True
else:
print("User or Pass invalid, please try again.")
login()
def home():
auth = False
i = input("Press 1 to login, or 2 to make a new account.")
if i == '1':
login()
elif i == '2':
newAccount()
else:
print("Please choose an appropriate answer")
home()
print("Welcome to generic website!")
home()
sql.py
Code:
import sqlite3
import sys
def printDB(): #for reference
conn = sqlite3.connect('files.db')
theCurser = conn.cursor()
result = conn.execute("SELECT ID, Username, Password FROM Accounts")
for row in result:
print("Account Number: ", row[0])
print("Username: ", row[1])
print("Password: ", row[2])
conn.close()
def create():
try:
conn.execute("CREATE TABLE Accounts(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Username TEXT, Password TEXT);")
conn.commit()
print("Table Created")
except sqlite3.OperationalError:
print("")
def add(un, pw):
conn = sqlite3.connect('files.db')
theCurser = conn.cursor()
varU = (un,) #I think I can take these out but I'm scared to.
varP = (pw,)
conn.execute("INSERT INTO Accounts(Username, Password) VALUES (?, ?)", (un, pw))
conn.commit()
conn.close()
def searchDB(usn, psw):
conn = sqlite3.connect('files.db')
cursor = conn.cursor()
h = cursor.execute("SELECT Username, Password FROM Accounts")
j = cursor.fetchall()
for row in j:
if row[0] == usn and row[1] == psw:
conn.close()
return True
conn.close()
return False
Last edited:






