Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3 #Used for making the database of the usernames and passwords
- import time #Is used to be converted into a timestamp
- import datetime #Converts the unix value into a date
- currentuser=""
- def clear():
- print("\n"*50)
- conn = sqlite3.connect("logins.db") #Makes the .db file
- c = conn.cursor() #Makes a cursor object to execute SQL code
- def create_table():
- c.execute("CREATE TABLE IF NOT EXISTS logins(user TEXT, pass TEXT)")
- #This creates a table if it doesn't already exist with the values of a username
- #password and datestamp
- def username_increase(u):
- ul=list(u)
- num = ul[-2:]
- ul.pop()
- ul.pop()
- num = int("".join(num))
- num+=1
- num=str(num)
- ul.append(num)
- newusername="".join(ul)
- return newusername
- def make_username(n,a):
- n=n.capitalize()
- username=n[:3]+str(a)
- return username
- def quiz(difficulty,topic):
- pass
- def topic_select():
- pass
- def difficulty_select():
- difficulties = ["Easy", "Meduim","Hard"]#For return
- print("What difficulty would you like?")
- for i in range(3):
- print("%s) %s"%(i+1,difficulties[i]))
- while True: #Input and validation
- try:
- choice = int(input(">>>"))
- if (choice<1) or (choice>3):
- print("Invalid input")
- else:
- break
- except ValueError:
- print("Invalid input")
- continue
- except KeyboardInterrupt:
- print("Invalid input")
- continue
- return difficulties[choice-1]#Returns string depending on choice
- def login():
- global currentuser
- while True: #Username entry
- try:
- usr=input("Enter username: ")
- if not usr:
- print("Invalid input")
- #clear()
- continue
- else:
- c.execute("SELECT * FROM logins WHERE user=?",
- (usr,)) #To see if an account with that username exists
- data = c.fetchall()
- if len(data)==0:
- print("That username doesn't exist") #Error message
- continue
- else:
- break
- except KeyboardInterrupt:
- print("Invalid input")
- clear()
- continue
- #clear()
- while True: #Password entry
- try:
- psw = input("Enter password: ")
- if not psw:
- print("Invalid input")
- #clear()
- continue
- else:
- c.execute("SELECT * FROM logins WHERE user =? AND pass=?",
- (usr,psw)) #To see if an account with that username and password exists
- data=c.fetchall()
- if len(data)==0:
- print("Password is incorrect")
- continue
- else:
- break
- #clear()
- except KeyboardInterrupt:
- print("Invalid input")
- clear()
- continue
- currentuser=usr #Assigns the currentuser global variable to the username that was just logged into
- quiz(difficulty_select(),topic_select())
- def create_account():
- while True:
- try:
- name = input("Give me your name: ")#Entry of name for the username
- if not name.isalpha(): #Validation \/ #########
- print("That is invalid")
- continue
- elif not name:
- print("That is invalid")
- elif len(name)<3:
- print("Invalid")
- continue
- else:
- break
- except KeyboardInterrupt:
- print("Invalid input")
- continue#######################################
- while True:
- try:
- age = int(input("Give me your age: "))#Entry of age for the username
- if len(str(age))>2: #Validation \/
- print("Invalid")
- continue
- elif not age:
- print("Invalid")
- continue
- else:
- break
- except KeyboardInterrupt:
- print("Invalid")
- except ValueError:
- print("Invalid")
- continue ####################################
- username = make_username(name,age)
- while True: #Password input
- try:
- password = input("Enter a password: ")
- if len(password)<6: #validation \/############
- print("That password is too small")
- continue
- else:
- password2 = input("Please enter it again: ")
- if password != password2:
- print("That is incorrect")
- continue
- else:
- break
- except KeyboardInterrupt:
- print("Invalid input")#######################
- continue
- password2 = input("Please enter it again: ") #second password to ensure user has no typos
- if password != password2:
- print("That is incorrect")
- continue
- changed=0
- while True:# Loop that checks the SQL database if the username generated already exists within the database
- c.execute("SELECT * FROM logins WHERE user=?",
- (username,))
- data=c.fetchall()
- if len(data)==0: #If it doesn't exist
- c.execute("INSERT INTO logins(user,pass) VALUES(?, ?)",
- (username,password)) #Adding the username
- conn.commit()#Save
- break
- else:
- username = username_increase(username)#change the username by increasing the last two digits
- changed=1
- continue
- if changed==1:
- print("That username already existed, so your new one is %s"%(username))
- else:
- print("Your username is %s"%(username))
- def start_menu():
- while True:
- try:
- clear()
- print("1) Login\n2) Create an account\n3) Quit")
- choice = int(input(">>>"))#Enters number
- if (choice >2) or (choice<1):#Checks if it is within the bounds
- print("Invalid input")#Error message
- continue
- else:
- break
- except KeyboardInterrupt: #Stops CTRL-C crash and checks if the input was a number
- print ("Invalid input")
- except ValueError:
- print("Invalid input")
- if choice ==1:
- login()
- elif choice ==2:
- create_account()
- elif choice ==3:
- c.close() #closes connections to the database to free memory in the computer
- conn.close()
- quit() #Shuts down the program
- create_table()
- start_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement