Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import statistics as s #To create maths qs. like mean, median etc.. and allows me to write statistic as s to decrease typing time
  2. import random #random letters to add to a name (to make it unique), also random questions
  3. import string
  4. import sys #system/ sys.end to end the program
  5. import time
  6. import json
  7.  
  8. def adlogin():#This is the function responsible for the login of the admin
  9.     print("Please enter the administrator's username")
  10.  
  11. def stlogin():#This is the function responsible for the login of students
  12.     print ("you have chosen to login")
  13.  
  14.     with open("file.txt") as read:
  15.  
  16.         username = input("Username:")
  17.  
  18.     try:
  19.         data = json.load(read)
  20.  
  21.     except ValueError:
  22.         print("no users registered. Sign up!!!")
  23.  
  24.     try:
  25.         data = json.load(read)  
  26.  
  27.     except json.decoder.JSONDecodeError as je:
  28.         print('invalid json: %s' % je)
  29.         print ("no users registered. Sign up!!!")
  30.         time.sleep(1)
  31.         entrance()
  32.  
  33.  
  34.     data = json.load(read)
  35.     inputted_user = input("username")
  36.     if inputted_user in open('file.txt').read():
  37.         if type(inputted_user) == str:
  38.             account = json.loads(inputted_user)
  39.             inputted_password = input(" Password: ")
  40.             if inputted_password == account["password"]:
  41.                 print("logged in")
  42.             else:
  43.                 print("incorrect login")
  44.         else:
  45.          print("incorrect login")
  46.     else:
  47.         print("incorrect login")
  48.        
  49.        
  50.        
  51. def signup():
  52.     print ("you have chosen to sign up.")
  53.    
  54.     name = input("Please enter your name.\n {Name}: ")
  55.    
  56.     nameforinfo = name
  57.        
  58.     age = input("Please enter your age:\n[This program is optimised for those 11-17]\n{age}: ")
  59.    
  60.     while int(age) < 10 or int(age) > 18:# loop created until a valid age entered.
  61.        age = input("What is your age?\n{age}:  ")
  62.        
  63.     username = (name[0:3] + age)
  64.    
  65.     while username in open('userinfo.txt').read():#checking the availability of username
  66.         username += random.choice(string.ascii_letters)
  67.        
  68.     print ("your username is " + username)
  69.    
  70.     password = input("Make a password (longer than 5 letters).\n{password}:")
  71.    
  72.     while len(password) < 3:
  73.         print("password is too short")
  74.         password = input("Make a password (longer than 5 letters).\n{password}:")
  75.     userinfo = open("userinfo.txt", 'w')
  76.     userinfo.write("User's Info\n")
  77.     userinfo.close()
  78.     with open("userinfo.txt", 'w') as w:
  79.  
  80.         w.write(
  81.             json.dumps(
  82.                 {
  83.                       "user": {
  84.                                     "username": username,
  85.                                     "name": nameforinfo,
  86.                                     "age" : age,
  87.                                     "password": password
  88.                             }
  89.                  }
  90.             )
  91.         )
  92.     entrance()
  93.  
  94. def close():#This is the function which quits the program.
  95.     closebinary =input("Are you sure you want to quit? {yes} {no}\n{yes/no}: ")
  96.     if closebinary == "yes":
  97.         print("See you next time")
  98.         time.sleep(2)
  99.         sys.exit
  100.     elif closebinary == "no":
  101.         print("Glad you're staying with us.")
  102.         time.sleep(2)
  103.         entrance()
  104.     else:
  105.         invalid_answer()
  106.  
  107. def invalid_answer():
  108.     print("Thats not a valid answer.\nI'll prompt you to the start.")
  109.     time.sleep(2)
  110.     entrance()
  111.  
  112. def entrance():
  113.     print("Hello and welcome to this quiz")
  114.    
  115.        
  116.     answer = input("{1} Student Login\n{2}Admin Login:\n{3}Signup\n{4}Exit\n{#}: ")
  117.     if answer == "1":
  118.         stlogin()
  119.     elif answer == "2" :
  120.         adlogin()
  121.     elif answer == "3" :
  122.         signup()
  123.     elif answer == "4" :
  124.         close()
  125.     else:
  126.         invalid_answer()
  127.         entrance()
  128.  
  129.  
  130. entrance()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement