0% found this document useful (0 votes)
14 views

lessssGo_V3

Program

Uploaded by

kunalffserver1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

lessssGo_V3

Program

Uploaded by

kunalffserver1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#program for sign up and login using mysql data base system.

import mysql.connector as sql

def Create_Connection():
"""Create and return a connection to the database."""
con = sql.connect(host = "localhost", username = "root", passwd =
"9628", database = "riteshgamestoretest") #Replace ‘riteshgamestoretest’
with your datbase.
if not con.is_connected():
print("Connection to database failed.")
exit(0)
return con

def Fetch_Usernames():
try:
conn = Create_Connection()
cursor = conn.cursor(buffered = True)
query = "SELECT username,password FROM users" #Replace 'Users’ with
your actual table name
cursor.execute(query)
usernames = cursor.fetchall()

usrname = {}
for i,j in usernames:
usrname[i] = j

except Exception as e:
print("==============================")
print(f"An error occurred: {e}")
print("==============================")
finally:
# Close the cursor and connection
cursor.close()
conn.close()
return usrname

def Validate_Email(email):
if "@" in email and "." in email:
if len(email.split("@")[0]) < 4 or len(email.split("@")[1]) < 3:
return False
else:
True
else:
return False

def Validate_password(passwd):
'''For passsword validation'''
l = u = d = s = 0 # lowercase, uppercase, digit, specialchr.
if len(passwd) <= 8 and len(passwd) >= 20:
return False
else:
for i in passwd:
if i.isalpha():
if i.islower():
l += 1
else:
u += 1
elif i.isdigit():
d += 1
else:
s += 1

if l >= 1 and u >= 1 and d >= 1 and s >= 1:


return True
else:
return False

def Insert_Record(name, username, password, email):


try:
conn = Create_Connection()
cursor = conn.cursor()
query = f"INSERT INTO Users VALUES('{name}', '{username}',
'{password}', '{email}')"
cursor.execute(query)
conn.commit()

except Exception as e:
print("==============================")
print(f"An error occurred: {e}")
print("==============================")

finally:
# Close the cursor and connections
cursor.close()
conn.close()

def SignUp():
try:
while True:
print("==============================")
name = input("Enter your name: ")

email = input("Enter your email: ")


while Validate_Email(email):
print("==============================")
print("Invalid email! Username must be at least 4 characters
and domain must be at least 3 characters.")
email = input("Enter your email: ")

username = input("Enter your username: ")


usrnmaes = Fetch_Usernames()
while (username in usrnmaes.keys()):
print("==============================")
print("Username already exists! Please choose another one.")
username = input("Enter your username: ")

password = input("Enter your password: ")


while not Validate_password(password):
print("==============================")
print("Invalid password! Password must be between 9 and 19
characters long, and contain at least one digit, one uppercase letter, one
lowercase letter, and one special character.")
password = input("Enter your password: ")
else:
confirm_password = input("Confirm your password: ")
while password != confirm_password:
print("==============================")
print("Password do not match!")
confirm_password = input("Confirm your password: ")
else:
print("SignUp succesful!")
break

except Exception as e:
print("==============================")
print(f"An error occurred: {e}")
print("==============================")

Insert_Record(name, username, password, email)

def LogIn():
try:
while True:
print("=======WELCOME BACK=======")
username = input("Enter your username: ")
usrnmaes = Fetch_Usernames()
while not (username in usrnmaes.keys()):
print("==============================")
print("Username does not exist!")
username = input("Enter your username(0-exit): ")
if username == "0":
break
else:
password = input("Enter your password: ")
if usrnmaes[username] == password:
print("LogIn succesful!")
break
else:
print("==============================")
print("Wrong password!")

except Exception as e:
print("==============================")
print(f"An error occurred: {e}")
print("==============================")

def Menu():
while True:
print("1. SignUp")
print("2. LogIn")
print("3. Exit")
try:
choice = int(input("Enter your choice: "))
if choice == 1:
SignUp()
elif choice == 2:
LogIn()
elif choice == 3:
exit(0)
else:
raise ValueError
except ValueError:
print("Invalid choice!")
Menu()

You might also like