Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import sqlite3 #Used for making the database of the usernames and passwords
  2. import time #Is used to be converted into a timestamp
  3. import datetime #Converts the unix value into a date
  4. currentuser=""
  5. def clear():
  6. print("\n"*50)
  7. conn = sqlite3.connect("logins.db") #Makes the .db file
  8. c = conn.cursor() #Makes a cursor object to execute SQL code
  9. def create_table():
  10. c.execute("CREATE TABLE IF NOT EXISTS logins(user TEXT, pass TEXT)")
  11. #This creates a table if it doesn't already exist with the values of a username
  12. #password and datestamp
  13. def username_increase(u):
  14. ul=list(u)
  15. num = ul[-2:]
  16. ul.pop()
  17. ul.pop()
  18. num = int("".join(num))
  19. num+=1
  20. num=str(num)
  21. ul.append(num)
  22. newusername="".join(ul)
  23. return newusername
  24. def make_username(n,a):
  25. n=n.capitalize()
  26. username=n[:3]+str(a)
  27. return username
  28.  
  29. def quiz(difficulty,topic):
  30. pass
  31. def topic_select():
  32. pass
  33.  
  34.  
  35. def difficulty_select():
  36. difficulties = ["Easy", "Meduim","Hard"]#For return
  37. print("What difficulty would you like?")
  38. for i in range(3):
  39. print("%s) %s"%(i+1,difficulties[i]))
  40. while True: #Input and validation
  41. try:
  42. choice = int(input(">>>"))
  43. if (choice<1) or (choice>3):
  44. print("Invalid input")
  45. else:
  46. break
  47. except ValueError:
  48. print("Invalid input")
  49. continue
  50. except KeyboardInterrupt:
  51. print("Invalid input")
  52. continue
  53. return difficulties[choice-1]#Returns string depending on choice
  54.  
  55. def login():
  56. global currentuser
  57. while True: #Username entry
  58. try:
  59. usr=input("Enter username: ")
  60. if not usr:
  61. print("Invalid input")
  62. #clear()
  63. continue
  64. else:
  65. c.execute("SELECT * FROM logins WHERE user=?",
  66. (usr,)) #To see if an account with that username exists
  67. data = c.fetchall()
  68. if len(data)==0:
  69. print("That username doesn't exist") #Error message
  70. continue
  71. else:
  72. break
  73. except KeyboardInterrupt:
  74. print("Invalid input")
  75. clear()
  76. continue
  77. #clear()
  78. while True: #Password entry
  79. try:
  80. psw = input("Enter password: ")
  81. if not psw:
  82. print("Invalid input")
  83. #clear()
  84. continue
  85. else:
  86. c.execute("SELECT * FROM logins WHERE user =? AND pass=?",
  87. (usr,psw)) #To see if an account with that username and password exists
  88. data=c.fetchall()
  89. if len(data)==0:
  90. print("Password is incorrect")
  91. continue
  92. else:
  93. break
  94. #clear()
  95. except KeyboardInterrupt:
  96. print("Invalid input")
  97. clear()
  98. continue
  99. currentuser=usr #Assigns the currentuser global variable to the username that was just logged into
  100. quiz(difficulty_select(),topic_select())
  101.  
  102. def create_account():
  103. while True:
  104. try:
  105. name = input("Give me your name: ")#Entry of name for the username
  106. if not name.isalpha(): #Validation \/ #########
  107. print("That is invalid")
  108. continue
  109. elif not name:
  110. print("That is invalid")
  111. elif len(name)<3:
  112. print("Invalid")
  113. continue
  114. else:
  115. break
  116. except KeyboardInterrupt:
  117. print("Invalid input")
  118. continue#######################################
  119. while True:
  120. try:
  121. age = int(input("Give me your age: "))#Entry of age for the username
  122. if len(str(age))>2: #Validation \/
  123. print("Invalid")
  124. continue
  125. elif not age:
  126. print("Invalid")
  127. continue
  128. else:
  129. break
  130. except KeyboardInterrupt:
  131. print("Invalid")
  132. except ValueError:
  133. print("Invalid")
  134. continue ####################################
  135. username = make_username(name,age)
  136. while True: #Password input
  137. try:
  138. password = input("Enter a password: ")
  139. if len(password)<6: #validation \/############
  140. print("That password is too small")
  141. continue
  142. else:
  143. password2 = input("Please enter it again: ")
  144. if password != password2:
  145. print("That is incorrect")
  146. continue
  147. else:
  148. break
  149. except KeyboardInterrupt:
  150. print("Invalid input")#######################
  151. continue
  152.  
  153. password2 = input("Please enter it again: ") #second password to ensure user has no typos
  154. if password != password2:
  155. print("That is incorrect")
  156. continue
  157. changed=0
  158. while True:# Loop that checks the SQL database if the username generated already exists within the database
  159. c.execute("SELECT * FROM logins WHERE user=?",
  160. (username,))
  161. data=c.fetchall()
  162. if len(data)==0: #If it doesn't exist
  163.  
  164. c.execute("INSERT INTO logins(user,pass) VALUES(?, ?)",
  165. (username,password)) #Adding the username
  166. conn.commit()#Save
  167. break
  168. else:
  169. username = username_increase(username)#change the username by increasing the last two digits
  170. changed=1
  171. continue
  172. if changed==1:
  173. print("That username already existed, so your new one is %s"%(username))
  174. else:
  175. print("Your username is %s"%(username))
  176.  
  177.  
  178. def start_menu():
  179. while True:
  180. try:
  181. clear()
  182. print("1) Login\n2) Create an account\n3) Quit")
  183. choice = int(input(">>>"))#Enters number
  184. if (choice >2) or (choice<1):#Checks if it is within the bounds
  185. print("Invalid input")#Error message
  186. continue
  187. else:
  188. break
  189. except KeyboardInterrupt: #Stops CTRL-C crash and checks if the input was a number
  190. print ("Invalid input")
  191. except ValueError:
  192. print("Invalid input")
  193. if choice ==1:
  194. login()
  195. elif choice ==2:
  196. create_account()
  197. elif choice ==3:
  198. c.close() #closes connections to the database to free memory in the computer
  199. conn.close()
  200. quit() #Shuts down the program
  201.  
  202. create_table()
  203. start_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement